我可以使用 formControl valueChanges 来检测对 mat-chip-list 的更改吗?
Can I use formControl valueChanges to detect changes to a mat-chip-list?
我有一个 mat-chip-list
并想检测它何时发生变化,即:添加或删除的项目。
我希望我可以将表单控件附加到它并利用 valueChanges
observable 来实现这一点,即:
<mat-chip-list #chipList [formControl]="chipListCtrl">
chipListCtrl = new FormControl();
...
this.fruitCtrl.valueChanges.pipe(tap(value => console.log('fruitCtrl: ', value))).subscribe();
但是,这不会将任何内容记录到控制台。
参考Stackblitz输入控件以这种方式成功工作,但芯片列表没有。
这可能吗?如果可以,我需要做什么才能让它发挥作用?
您不需要 formControl 来检测 mat-chip-list 中的更改。
根据文档 - https://material.angular.io/components/chips/api,
你可以直接使用(更改)eventemitter。
@Output()
变化:EventEmitter
用户更改所选芯片列表值时发出的事件。
芯片列表不会发出 valueChanges,因为您没有更新它的值。您正在更新 fruits
数组,它与您的 formControl 没有任何联系。
我已经分叉了你的 stackblitz 以便事件更新你的 chipListCtrl
的值并且 mat-chip-list
的内容正在使用 chipListCtrl.value
。
我有一个 mat-chip-list
并想检测它何时发生变化,即:添加或删除的项目。
我希望我可以将表单控件附加到它并利用 valueChanges
observable 来实现这一点,即:
<mat-chip-list #chipList [formControl]="chipListCtrl">
chipListCtrl = new FormControl();
...
this.fruitCtrl.valueChanges.pipe(tap(value => console.log('fruitCtrl: ', value))).subscribe();
但是,这不会将任何内容记录到控制台。
参考Stackblitz输入控件以这种方式成功工作,但芯片列表没有。
这可能吗?如果可以,我需要做什么才能让它发挥作用?
您不需要 formControl 来检测 mat-chip-list 中的更改。 根据文档 - https://material.angular.io/components/chips/api, 你可以直接使用(更改)eventemitter。
@Output()
变化:EventEmitter
用户更改所选芯片列表值时发出的事件。
芯片列表不会发出 valueChanges,因为您没有更新它的值。您正在更新 fruits
数组,它与您的 formControl 没有任何联系。
我已经分叉了你的 stackblitz 以便事件更新你的 chipListCtrl
的值并且 mat-chip-list
的内容正在使用 chipListCtrl.value
。