Angular 8 - *ngIf 和 mat-checkbox 不工作

Angular 8 - *ngIf and mat-checkbox not working

我在使用 Angular Material 复选框时遇到问题,我认为这是一件相对简单的事情,但由于某种原因我无法让它工作。

我有一个 div ,我只想在选中 mat-checkbox 时显示它,但它似乎不想与 *ngIf 一起使用,即使我在这里找到了它的工作示例: https://stackblitz.com/edit/angular-mat-checked?file=app%2Fcheckbox-overview-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 应该相对简单,但我似乎被难住了。我可能遗漏了一些非常明显的东西,但我看不出它可能是什么。

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

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

您不需要在组件中定义变量。您还可以参考复选框

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