gpt4 book ai didi

html - *ngIf...当数据为空时,为mat-checkbox设置disabled=true

转载 作者:行者123 更新时间:2023-11-28 05:10:19 26 4
gpt4 key购买 nike

我有以下代码:

<div class="item-container">
<mat-checkbox class="item">{{contactInfo.privateMobile}}</mat-checkbox>
</div>

如果contactInfo.privateMobile为空,我不希望用户能够选中该复选框。

我已经尝试过类似的方法:<mat-checkbox *ngIf={{contactInfo.privateMobile}} === null disabled=true" class="item">{{contactInfo.privateMobile}}</mat-checkbox> ,但这不起作用或无法编译。也许我需要在我的 TypeScript 代码中使用一个方法?不确定如何去做。

最佳答案

不必在 *ngIf 中使用 {{}}:

<div class="item-container">
<mat-checkbox *ngIf="contactInfo.privateMobile" class="item">{{contactInfo.privateMobile}}</mat-checkbox>
</div>

如果你只想禁用它,你可以这样做:

<div class="item-container">
<mat-checkbox [disabled]="!contactInfo.privateMobile" class="item">{{contactInfo.privateMobile || '(private mobile unavailable)'}}</mat-checkbox>
</div>

演示:https://stackblitz.com/edit/angular-h1sfoy?file=app%2Fcheckbox-overview-example.html

关于html - *ngIf...当数据为空时,为mat-checkbox设置disabled=true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58196703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com