且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular 2+材料垫-芯片列表formArray验证

更新时间:2023-11-02 17:04:34

问题是,当chipList的FormArray状态为INVALID时,未将chipList的errorState设置为true.

The problem is that the chipList's errorState isn't set to true when the chipList's FormArray status is INVALID.

我正面临着同样的问题,不知道为什么这不能立即解决,或者如何使用chipList的形式为FormArray隐式实现.

I'm facing the same problem and don't know why this isn't working out of the box or how this can be achieved implicitly with the chipList's form being a FormArray.

作为一种解决方法,您可以从FormArray听状态更改并手动设置chipList的errorState:

As a workaround you can listen to status changes from the FormArray and set the chipList's errorState manually:

@ViewChild('chipList') chipList: MatChipList;

ngOnInit() {
  this.myForm.get('names').statusChanges.subscribe(
    status => this.chipList.errorState = status === 'INVALID'
  );
}

https://stackblitz.com/edit/angular-4d5vfj-gywxjz