我正在使用 formArray 创建动态表单。但是我遇到了“类型错误:无法读取未定义的属性‘控件’”
enter code here
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, FormArray } from '@angular/forms';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'Trainner Registration Form ';
registrationForm: FormGroup;
get LanguagesForm() {
return this.registrationForm.get('Languages') as FormArray;
}
addLanguage() {
this.LanguagesForm.push(this.fb.control(''));
}
constructor(private fb : FormBuilder ) {}
ngOnInit(){
this.registrationForm = this.fb.group({
personalDetails : this.fb.group({
name: this.fb.group({
firstName: [''],
lastName: ['']
}),
aboutYours: [''],
dob: [''],
// lang: [''],
Languages: this.fb.array([]),
wTT: ['']
})
});
}
onSubmit() {
console.log(this.registrationForm.value);
// this._registerationservice.register(this.registrationForm.value).subscribe(
// response => console.log('Success', response),
// error => console.log('Error',error)
// );
}
}
预期结果:如果用户单击“添加语言”按钮,则应创建一个新的输入字段。
实际结果:我收到“类型错误:无法读取未定义的属性‘控件’”
牛魔王的故事
潇潇雨雨
相关分类