2017-07-16 75 views
1

这已被问了几次,但其他示例似乎比我的简单用例更复杂一点。Angular 4 * ngIf - 动态显示/隐藏模型变量变化

我试图根据选择框的值显示/隐藏textarea

它在加载时按预期工作,但在更改来回选择的值时不起作用。

正如我所说的,模型变量的默认值是false,并且textarea在加载时隐藏(根据需要)。

下面是HTML:

<div> 
    <select id="isFunded" [(ngModel)]="isFunded" name="isFundedSelect"> 
     <option value="false" selected>No</option> 
     <option value="true">Yes</option> 
    </select> 
</div> 
<div> 
    <textarea class="form-control" rows="3" placeholder="Notes..." *ngIf="isFunded"></textarea> 
</div> 
<p>Is funded? {{isFunded}}</p> <!-- this updates when the select value changes --> 

这里是我的组件的整个身体:

import { Component, OnInit } from '@angular/core'; 

@Component({ 
    selector: 'app-second-form', 
    templateUrl: './second-form.component.html', 
    styleUrls: ['./second-form.component.sass'] 
}) 
export class SecondFormComponent implements OnInit { 
    isFunded = false; 
    constructor() { } 
    ngOnInit() { 
    } 
} 

我怎样才能重新隐藏textarea更改为false再回到true后?

如果是培训相关,我有角CLI生成的项目,这些都是在我的应用程序模块进口:BrowserModule, FormsModule, CommonModule

谢谢!

回答

4

尝试使用[ngValue] ='true'而不是值。

+0

这样做。谢谢! – Mac