简体   繁体   English

垫芯片中(单击)中的ExpressionChangedAfterItHasBeenCheckedError

[英]ExpressionChangedAfterItHasBeenCheckedError in (click) for mat-chip

I'm getting a ExpressionChangedAfterItHasBeenCheckedError when trying to set a value on a click event for a mat-chip 尝试在mat-chip的点击事件上设置值时,出现ExpressionChangedAfterItHaHasBeenCheckedError

I'm created a stackblitz to see the error in action (Open the console then click two or more chips): https://stackblitz.com/edit/angular-ddapw1?file=app/chips-stacked-example.html 我创建了一个stackblitz来查看操作中的错误(打开控制台,然后单击两个或更多筹码): https ://stackblitz.com/edit/angular-ddapw1?file=app/chips-stacked-example.html

// index.html
<mat-chip-list>
  <mat-chip
      *ngFor="let chip of availableItems; index as idx"
      [selected]="selectedItems.indexOf(idx) > -1"
      (click)="select(idx)">
    {{chip}}
  </mat-chip>
</mat-chip-list>

// index.js
import {Component, ChangeDetectorRef} from '@angular/core';

@Component({
  selector: 'chips-stacked-example',
  templateUrl: 'chips-stacked-example.html',
  styleUrls: ['chips-stacked-example.css'],
})
export class ChipsStackedExample {
  constructor(private cdr: ChangeDetectorRef) {}
  availableItems = ['foo', 'bar', 'baz'];
  selectedItems = [];
  select(idx) {
    console.log('selecting', idx);
    this.selectedItems.push(idx);
    //this.cdr.detectChanges()
  }
}

您忘记添加multiple属性:

<mat-chip-list multiple>

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

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