在代码中使用了表单(form)控件,但是在模板中找不到相应的控件,导致报错。解决方法是检查模板中的控件名是否与代码中一致,或者在代码中添加相应的控件。比如,在这个例子中,需要检查 childrenFormArray 和 gender 这两个控件名是否在模板中正确声明。
代码示例:
HTML 模板:
Male
Female
Typescript 代码:
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, FormArray, FormControl } from '@angular/forms';
@Component({
selector: 'app-child-form',
templateUrl: './child-form.component.html',
styleUrls: ['./child-form.component.css']
})
export class ChildFormComponent {
childrenFormArray: FormArray;
constructor(private fb: FormBuilder) {
this.childrenFormArray = this.fb.array([
this.fb.group({
name: [''],
age: [''],
gender: ['']
})
]);
}
}