Gio*_*i B 2 foreach select inotifypropertychanged checked angular
我在 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 方法,但没有一个能解决问题。
我该如何解决这个问题?
当您有多个选定的芯片时会出现此问题。如果你想多选,你必须设置[multiple]="true"上mat-chip-list
<mat-chip-list [multiple]="true">
<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>
</mat-chip-list>
Run Code Online (Sandbox Code Playgroud)
演示:https : //stackblitz.com/edit/angular-x7tkwc
| 归档时间: |
|
| 查看次数: |
1348 次 |
| 最近记录: |