以我的项目作为示例,总结一下Angular响应式表单的应用和常用的方法:
1.创建表单
form.ts代码
import { Component, OnInit } from "@angular/core";
import { FormArray, FormBuilder, FormControl, FormGroup, Validators } from "@angular/forms";
@Component({
selector: "app-form",
templateUrl: "./form.component.html"
})
export class SLSDetailComponent implements OnInit{
slsForm: FormGroup;//定义表单slsForm
ngOnInit(): void {
this.createForm();//初始化创建表单
}
createForm() {//创建项目需要的数据(多层嵌套的数据)
this.slsForm = this.fb.group({
UserServiceDescription: this.fb.group({}),
VPL: this.fb.group({})
});
}
}