我有/没有单选按钮,我正在读取存储在JSON对象中的值。首先,我要检查json对象是否为空或者单选按钮的字段是否为空。在那种情况下,我正在显示两个单选按钮未选中。接下来,我正在检查json对象中的字段是否显示真值,如果是,则检查yes单选按钮,然后检查false值并检查no单选按钮。看起来我在视图中使用了很多相同或类似的代码,我想知道是否有更有效的方法来做到这一点。这是我的代码的样子。感谢。
<!-- json array will be empty when first coming to page, display no boxes checked -->
<div *ngIf="(questions | json) === '{}' || questions.partYear == null">
<input type="radio" id="yesRadio" formControlName="partYear" name="partYear" value="yes" [(ngModel)]="questions.partYear">Yes<br/>
<input type="radio" id="noRadio" formControlName="partYear" name="partYear" value="no" [(ngModel)]="questions.partYear">No
</div>
<!-- user comes back to page, populate box that was checked during sesssion -->
<div *ngIf="questions?.partYear === 'yes' || questions?.partYear === true">
<input type="radio" id="yesRadio" formControlName="partYear" name="partYear" value="yes" checked [(ngModel)]="questions.partYear">Yes
</div>
<div *ngIf="questions?.partYear === 'no' || questions?.partYear === false">
<input type="radio" id="yesRadio" formControlName="partYear" name="partYear" value="yes" [(ngModel)]="questions.partYear">Yes
</div>
<div *ngIf="questions?.partYear === 'yes' || questions?.partYear === true">
<input type="radio" id="noRadio" formControlName="partYear" name="partYear" value="no" [(ngModel)]="questions.partYear">No
</div>
<div *ngIf="questions?.partYear === 'no' || questions?.partYear === false ">
<input type="radio" id="noRadio" formControlName="partYear" name="partYear" value="no" checked [(ngModel)]="questions.partYear">No
</div>
答案 0 :(得分:0)
你应该可以做这样的事情并且完全不需要任何* ngIf语句。
!=