Angular11 如何初始化Child的formgroup值?
Angular 11 how to initialize the Child's formgroup values?
Parent HTML 定义如下 -
<div class="modal-body">
<app-addchild [childform]="element"></app-addchild>
</div>
这里的元素是JSON格式的行数据,有字段值。
childform 是定义为 @Input() 的 formgroup,子组件是这样的 -
@Input() public childform = new FormGroup({
code: new FormControl('', [Validators.required, Validators.pattern('[0-9]+')]),
countryCode: new FormControl('', Validators.required),
...
});
您可以像这样给 @Input
赋值。你可以这样做
@Input() set childform(value) {
// value is data passed to the input
// you can do something like this
this.gnpform.get('code').setValue(value.code);
}
public gnpform = new FormGroup({
code: new FormControl('', [Validators.required, Validators.pattern('[0-9]+')]),
countryCode: new FormControl('', Validators.required),
...
});
Parent HTML 定义如下 -
<div class="modal-body">
<app-addchild [childform]="element"></app-addchild>
</div>
这里的元素是JSON格式的行数据,有字段值。
childform 是定义为 @Input() 的 formgroup,子组件是这样的 -
@Input() public childform = new FormGroup({
code: new FormControl('', [Validators.required, Validators.pattern('[0-9]+')]),
countryCode: new FormControl('', Validators.required),
...
});
您可以像这样给 @Input
赋值。你可以这样做
@Input() set childform(value) {
// value is data passed to the input
// you can do something like this
this.gnpform.get('code').setValue(value.code);
}
public gnpform = new FormGroup({
code: new FormControl('', [Validators.required, Validators.pattern('[0-9]+')]),
countryCode: new FormControl('', Validators.required),
...
});