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

标签 html angular angular-material angular-ng-if

我有以下代码:

<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/

相关文章:

Angular 4动态填充查询参数

angular - 垫菜单中的 xPosition 和 yPosition 不起作用

html - 每个 angular2 组件的单独 CSS 文件会影响页面性能吗?

java - Angular 从 Spring RestController 获取图像并缓存它

javascript - JQuery json.filter 两个条件

声明了 Angular 5 'inputClick' 但它的值从未被读取

javascript - mdDatepicker 在 AngularJS 1.5.9 和 1.6 中被破坏

Angular Material Datepicker 不适合我

javascript - window.onload 和 getelementbyid 返回 null

html - 定位待办事项列表元素的问题