动态addControl到Formgroup Angular 5


77

尝试将新的formControl条目动态添加到Angular中的formGroup中。

method() {
  this.testForm.addControl('new', ('', Validators.required));
}

能做到吗?



就像Siro回答了您的问题一样,您可以使用addControl方法将新输入添加到表单组中。我有一个研究动态形式的小项目。我希望这会有所帮助。stackblitz.com/edit/angular-eypxbq?embed=1&file=src/app / ...
Michael Charles

Answers:



36

如果要FormBuilder用于表单,则还可以使用它来添加控件:

constructor(private fb: FormBuilder) { }
    
method() {
  this.testForm.addControl('new', this.fb.control('', Validators.required));
}

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.