首页 > 解决方案 > Angular - 我的 ngIf 仅隐藏一次输入标签

问题描述

*ngIf="currentIdea.oetSupportNeeded"我有一个包含 2 个值的下拉列表 - 是和否,以及另一个可以自由书写的输入标签。我只想input-label在用户在下拉列表中选择“是”时显示,并在选择“否”时隐藏它。

我的问题是以下代码只工作一次 - 当页面加载并选择“否”时,输入标签被隐藏,但是当您选择“是”然后再次选择“否”时,标签显示并且不会消失了。我希望它根据用户的选择打开/关闭

我尝试使用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>

标签: javascripthtmlangular

解决方案


According to the code you shared, the value of your select menu is bound to the booleanVariable property of your component. Your *ngIf queries another property, so the condition is wrong. You should be checking the booleanVariable in the condition.


推荐阅读