繁体   English   中英

Angular 8-* ngIf和mat-checkbox不起作用

[英]Angular 8 - *ngIf and mat-checkbox not working

我在Angular Material Checkbox中遇到问题,我认为这是相对简单的事情,但是由于某些原因,我无法使其正常工作。

我有一个div,我只想在选中mat-checkbox时显示,但是它似乎不希望与* ng一起使用,即使我在这里找到了它的一个示例: https ://stackblitz.com/ 编辑/角垫核对?文件=应用%2Fcheckbox-概述-example.html的

我尝试将非常相似的代码应用于我的项目,但是它似乎并不想工作,也没有尝试过其他任何事情。

        <mat-checkbox [(ngModel)]="checked" [checked]="false">
          I agree to {{clientName}} storing my details.
          <a href="#" target="_blank">View full terms & conditions</a>
        </mat-checkbox>

        <div *ngIf="checked" id="contactOptionsGroup">
          <p>Stay updated with news, features, and offers related to {{clientName}}.</p>
          <mat-checkbox>Phone</mat-checkbox>
          <mat-checkbox>Email</mat-checkbox>
        </div>

似乎能够通过选中和取消选中来显示和隐藏此div应该相对简单,但我似乎很困惑。 我可能会遗漏一些完全显而易见的内容,但看不到它可能是什么。

在项目的后面还会有该功能的另一个实例,因此它们将需要彼此独立地工作。

如果有人有帮助或建议,我将永远感激不已。

您无需在component中定义变量。 您也可以参考该复选框

<mat-checkbox #termsAndConditions>
          I agree to {{clientName}} storing my details.
          <a href="#" target="_blank">View full terms & conditions</a>
        </mat-checkbox>

        <div *ngIf="termsAndConditions.checked" id="contactOptionsGroup">
          <p>Stay updated with news, features, and offers related to {{clientName}}.</p>
          <mat-checkbox>Phone</mat-checkbox>
          <mat-checkbox>Email</mat-checkbox>
        </div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM