正如我先前在question
中提出的那样我想创建一个嵌套表单,其中父对象与孩子的formControlNames
无关
所以我们假设我们有一个组件componentA.component.ts
@Component({
selector: 'common-a',
template: `
<div [formGroup]="parentForm">
<div class="form-group">
<label>Common A[1]</label>
<div >
<input type="text" formControlName="valueA1">
<small>Description 1</small>
</div>
<div class="form-group">
<label>Common A[2]</label>
<div >
<input type="text" formControlName="valueA2">
<small>Description 2</small>
</div>
</div>
`
})
export class ComponentA implements OnInit{
@Input() parentForm: FormGroup;
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.parentForm.addControl("valueA1", new FormControl('', Validators.required));
this.parentForm.addControl("valueA2", new FormControl('', Validators.required));
}
}
主要成分。
@Component({
selector: 'main',
template: `
<form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
<div>
<div *ngFor="let c of myForm.controls.componentA.controls; let i=index" class="form-group">
<common-a [parentForm]="c"></common-a>
</div>
<div>
<button type="submit" [disabled]="myForm.invalid">Register!</button>
<a class="button" (click)="add_component()">Add New</a>
<a class="button" (click)="delete_component()">Delete</a>
</div>
</div>
<pre>form value: <br>{{myForm.value | json}}</pre>
</form>
`
})
export class MainComponent implements OnInit{
@Input('group') public myForm: FormGroup;
add_component() {
const control = <FormArray>this.myForm.controls['componentA'];
control.push(this._fb.group({}));
}
delete_component() {
const control = <FormArray>this.myForm.controls['componentA'];
control.removeAt(this.myForm.length-1);
}
constructor(private _fb: FormBuilder) {
}
ngOnInit() {
this.myForm = this._fb.group({
componentA : this._fb.array([this._fb.group({})])
});
}
onSubmit(formValue) {
console.log(formValue);
}
}
我现在的问题是如何使用patchValue
从服务器端点获取数据并填充表单数组的值,按需创建新的FormGroups
,始终与父级无关。
除了糟糕的架构外,这种方法的问题在于我用自己的实现覆盖patchValue方法并按需推送这些新组,因为在函数体内我知道有多少,但是孩子的OnInit将不会在函数内调用,将值保留为空。
var self = this;
this.formArray.patchValue = (value: {[key: string]: any}, {onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}) =>{
for(var i = 0; i < Object.keys(value).length; i++ ) {
self.add_new();
}
Object.keys(value).forEach(name => {
if (self.formArray.controls[name]) {
self.formArray.controls[name].patchValue(value[name], {onlySelf: true, emitEvent});
}
});
self.formArray.updateValueAndValidity({onlySelf, emitEvent});
}
我认为这是糟糕的设计。
static generate() {
return new FormGroup({
valueA1: new FormControl('', Validators.required),
valueA2: new FormControl('', Validators.required)
});
}
对于这类问题,您有什么清洁解决方案吗?
此外,基于我之前的问题,这是一个有效的plunkr
答案 0 :(得分:0)
动态控制的替代解决方案。
1:注入ChangeDetectorRef
constructor(private _changeDetectorRef: ChangeDetectorRef) { }
2:覆盖patchValue
this.control = new FormArray([]);
let patchValue = this.control.patchValue;
this.control.patchValue = (value: any, options?: Object) => {
for (let i = this.control.length; i < value.length; i++) {
// Push new item
}
this._changeDetectorRef.detectChanges();
patchValue.apply(this.control, [value, options]);
};