在Angular 7中,如果你遇到了"无法读取未定义的属性 'controls'"的错误,这意味着你正在尝试访问表单控件的controls属性,但该属性未定义。
为了解决这个问题,你可以按照以下步骤进行操作:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl } from '@angular/forms';
@Component({
selector: 'app-my-form',
templateUrl: './my-form.component.html',
styleUrls: ['./my-form.component.css']
})
export class MyFormComponent implements OnInit {
myForm: FormGroup;
ngOnInit() {
this.myForm = new FormGroup({
name: new FormControl(),
email: new FormControl()
});
}
}
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
@NgModule({
imports: [
FormsModule,
ReactiveFormsModule
]
})
export class AppModule { }
通过这些步骤,你应该能够解决"无法读取未定义的属性 'controls'"的错误,并正确读取表单控件的controls属性。