我在 Angular 中使用材料芯片作为标签,这些芯片由 *ngFor 循环。
<mat-chip class="c-pointer" *ngFor="let popularTag of popularTags; let i=index" [selected]="popularTag.is_user_tag" (click)="toggleTag(i)">#{{ popularTag.title }}</mat-chip>
Run Code Online (Sandbox Code Playgroud)
我需要在芯片点击时切换“is_user_tag”值,但它会引发错误
ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'mat-chip-selected: true'. Current value: 'mat-chip-selected: false'
Run Code Online (Sandbox Code Playgroud)
我尝试了 setTimeout() 和 ChangeDetectorRef 方法,但没有一个能解决问题。
我该如何解决这个问题?