订阅一组表单控件 - Angular 2/4

时间:2017-12-22 03:58:55

标签: angular angular2-forms angular4-forms

有没有办法订阅一个表单控件数组而不是多个get。在我的场景中我得到一个如图所示的对象 enter image description here 我想订阅textfielda,textfieldb和numbera值只更改。我尝试订阅整个表单的值更改(工作正常),但这是一个昂贵的操作,所以有任何解决方法来实现这个。这个字段是动态的所以我不能使用ngmodel 。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

这是我从我的代码中复制并编辑的。

表单数组中的每个表单组都有一个dataype:

newFormArray.push(this.fb.group({
  datatype: datatype,
  value: value
}));

这些是api调用的值,后端连接到db。

你现在做的是:

this.form.valueChanges.subscribe((item: any) => {
  if(item.array[i].datatype === 'textarea'){ //do something
  switch (item.array[i].datatype) { // or with a switch
        case 'textarea':
          //do textarea
          break;

HTML中的Ofc:

<div *ngIf="item.datatype === 'textarea'"> // show textarea