[英]Angular - my ngIf hides an input-label only once
*ngIf="currentIdea.oetSupportNeeded"
I have a dropdown list with 2 values - yes and no, and another input label where you can write freely. *ngIf="currentIdea.oetSupportNeeded"
我有一个包含2个值的下拉列表-是和否,以及另一个可以自由编写的输入标签。 I want to show the input-label
only when user chose "yes" in the dropdown list and to hide it when "no" is chosen. 我只想在用户在下拉列表中选择“是”时显示
input-label
在选择“否”时隐藏它。
My problem is that the following code works only once - when page loads and "no" is chosen, the input label is hidden, but when you choose "yes" and then "no" again, the label shows up and doesn't disappear anymore. 我的问题是,以下代码只能运行一次-当页面加载且选择“否”时,输入标签是隐藏的,但是当您选择“是”然后再次选择“否”时,标签显示并且不会消失不再。 I want it to toggle on/off depending on the user's choice
我希望它根据用户的选择打开/关闭
I tried using ngShow
, ngHide
, [disabled]
, etc. Nothing helped 我尝试使用
ngShow
, ngHide
, [disabled]
等。
<div class="select-wrapper" [ngClass]="{'select-wrapper-blocked': isNotAdmin()}">
<select class="input-control" [(ngModel)]="booleanVariable">
<option value="false">No</option>
<option value="true">Yes</option>
</select>
</div>
</div>
<div class="col form-input" [ngClass]="{'form-input-blocked': isNotAdmin()}">
<p class="input-label">
Some text
</p>
<input *ngIf="booleanVariable" class="input-control" [(ngModel)]="stringVariable" />
</div>
Use ngModelChange event and make a function to typecast the data. 使用ngModelChange事件并创建一个函数来强制转换数据。
Check the link for example. 例如,检查链接。 Show Hide Field using *ngIf
使用* ngIf显示隐藏字段
You are using true and false as strings. 您使用true和false作为字符串。 You can change the test into your ngIf.
您可以将测试更改为ngIf。
Like 喜欢
<select [(ngModel)]="foo">
<option [ngValue]="true">true</option>
<option [ngValue]="false">false</option>
</select>
<input *ngIf="foo == true"/>
And to define a default value, you can set the value of foo
into the class like 要定义默认值,您可以将
foo
的值设置为类似
public foo = true;
According to the code you shared, the value of your select menu is bound to the booleanVariable
property of your component. 根据您共享的代码,选择菜单的值绑定到组件的
booleanVariable
属性。 Your *ngIf
queries another property, so the condition is wrong. 您的
*ngIf
查询另一个属性,因此条件错误。 You should be checking the booleanVariable
in the condition. 您应该在条件中检查
booleanVariable
。
Something similar happened to me few hours ago. 几个小时前,我也发生了类似的事情。 The problem was that
问题是
<option value="false">No</option> <option value="true">Yes</option>
was wrong. <option value="false">No</option> <option value="true">Yes</option>
错误。 I solved it with 我解决了
<option [value]="false">No</option> <option [value]="true">Yes</option>
That's because otherwise Angular does not "analyze" the value of the option but recognizes it as a string. 那是因为否则Angular不会“分析”选项的值,而是将其识别为字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.