自定义表单组件
1 | @Component({ |
创建嵌套表单:
第一我们要创建一个完整的formGroup1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20...
// 父级组件里创建 form
this.form = this.fb.group({
selectedPizza: null,
pizzas: this.fb.array([]),
customerDetails: this.fb.group({
firstName: [null, Validators.required],
lastName: [null, Validators.required],
phoneNumber: [null, Validators.required],
address: this.fb.group({
street: [null, Validators.required],
houseNum: [null, Validators.required],
city: [null, Validators.required],
floor: [null, Validators.required],
})
})
}, {
validator: this.pizzaValidatorsService.formValidator()
});
...
1 | <div class="row"> |
子组件接受父级的 formGroup 然后正常使用
1 | @Component({ |
1 | <div class="CustomerDetails" [formGroup]="group"> |