Angular响应式表单 - 如何将valueChanges订阅添加到Form Array中的单个控件中
创始人
2024-10-30 05:30:18
0

要将valueChanges订阅添加到Form Array中的单个控件中,您可以按照以下步骤进行操作:

  1. 首先,在组件的初始化中创建一个Form Builder实例,并定义一个Form Array:
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormArray } from '@angular/forms';

@Component({
  selector: 'app-your-component',
  templateUrl: './your-component.component.html',
  styleUrls: ['./your-component.component.css']
})
export class YourComponent implements OnInit {
  myForm: FormGroup;

  constructor(private formBuilder: FormBuilder) { }

  ngOnInit() {
    this.myForm = this.formBuilder.group({
      controlArray: this.formBuilder.array([])
    });
  }
}
  1. 然后,您可以使用Form Array的push方法向其添加控件:
addControl() {
  const controlArray = this.myForm.get('controlArray') as FormArray;
  controlArray.push(this.formBuilder.control(''));
}
  1. 接下来,您可以使用Form Array的at方法获取单个控件,并订阅其valueChanges事件:
subscribeToValueChanges(index: number) {
  const controlArray = this.myForm.get('controlArray') as FormArray;
  const control = controlArray.at(index);

  control.valueChanges.subscribe(value => {
    // 在这里处理控件值的变化
    console.log(value);
  });
}
  1. 最后,在模板中,您可以使用ngFor来迭代Form Array中的控件,并将订阅函数添加到每个控件:

通过遵循以上步骤,您将能够将valueChanges订阅添加到Form Array中的单个控件中,并在控件的值发生变化时执行相应的操作。

相关内容

热门资讯

透视新版!wepoker有什么... 透视新版!wepoker有什么规律,wepoker私人局俱乐部怎么进(透视)详细教程(有挂工具)1、...
透视系统!wepoker有什么... 透视系统!wepoker有什么规律,wepoker有辅助功能吗(透视)科技教程(有挂教程)1、wep...
透视肯定!wepoker有没有... 透视肯定!wepoker有没有机器人,wepokerplus万能挂(透视)实用技巧(有挂教程)一、w...
透视教学!wepoker私人局... 透视教学!wepoker私人局辅助,wepoker辅助器激活码(透视)插件教程(有挂解密)1、任何w...
透视肯定!wepoker透视是... 透视肯定!wepoker透视是真的吗,wepoker手机版辅助(透视)必赢教程(有挂插件);一、we...
透视数据!wepoker怎么设... 透视数据!wepoker怎么设置房间,wepoker辅助透视(透视)新2025教程(有挂教程)1、在...
透视玄学!wepoker私人局... 透视玄学!wepoker私人局俱乐部,wepoker透视脚本免费下载pc(透视)AA德州教程(有挂介...
透视辅助!wepokerplu... 透视辅助!wepokerplus万能挂,如何下载wepoker安装包(透视)2025教程(有挂黑科技...
透视能赢!wepoker底牌透... 透视能赢!wepoker底牌透视脚本,wepoker数据分析工具(透视)第三方教程(有挂详情)1、实...
透视教学!wepokerplu... 透视教学!wepokerplus透视脚本免费,wepoker怎么获得好牌(透视)微扑克教程(有挂黑科...