【问题标题】:Detect changes of FormArray children检测 FormArray 子项的变化
【发布时间】:2019-02-15 04:07:22
【问题描述】:

我有一个FormArray,里面有不同的FormControl 项目。 我希望能够听到其中任何一个的更改事件。

我尝试过这样做:

this.form = this.fb.group({
    items: this.fb.array([this.fb.control('')])
});

(<FormArray>this.form.get('items')).controls.forEach((control: FormControl) => {
    control.valueChanges.subscribe(change => console.log(change));
});

但我似乎从未访问过console.log,尽管我确信子控件正在触发一个更改事件(来自ControlValueAccessor 界面)。

订阅FormArray的子控件更改的方法是什么

【问题讨论】:

  • 如果可能,只需订阅FormArray 上的更改。 // 顺便说一句,您当前的代码看起来应该可以工作 - 您也可以在这里发布模板吗?
  • 您需要检查表单数组控件的每个控件。因此,例如,如果您将控件作为项目 - (this.form.get('items')).controls.forEach( control => { control.get('item').valueChanges .debounceTime(100) 。 distinctUntilChanged() .subscribe(value => { this.onChanges(value); }); } );
  • 我同意 c69 - 您的代码看起来应该可以工作。检查这个问题和 epsilon 的答案 - 它对我有用:stackoverflow.com/questions/53654938/…

标签: angular typescript formarray


【解决方案1】:
 this.form.get('items').valueChanges.subscribe(changes=> {
     console.log(changes)
 })

【讨论】:

  • 你能提供你的问题的堆栈闪电吗?
  • 这是监听“项目”的变化,据我了解,Jago 需要分别监听所有控件。
  • 他要从其中任何一个,这只是一个例子,他可以写他想听的表单名称,只是this.form.valueChanges,来监听表单的整体变化
猜你喜欢
  • 2018-01-18
  • 1970-01-01
  • 2022-01-07
  • 2020-04-11
  • 2017-04-17
  • 2021-05-18
  • 2018-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多