我對嵌套模型驅動窗體沒有多少經驗,但是我找到了一些資源。
繼承人的通過實例的香餑餑:
<div formArrayName="addresses">
<div *ngFor="let address of myForm.controls.addresses.controls; let i=index" class="panel panel-default">
<div class="panel-heading">
<span>Address {{i + 1}}</span>
<span class="glyphicon glyphicon-remove pull-right" *ngIf="myForm.controls.addresses.controls.length > 1" (click)="removeAddress(i)"></span>
</div>
<div class="panel-body" [formGroupName]="i">
<div class="form-group col-xs-6">
<label>street</label>
<input type="text" class="form-control" formControlName="street">
<small [hidden]="myForm.controls.addresses.controls[i].controls.street.valid" class="text-danger">
Street is required
</small>
</div>
<div class="form-group col-xs-6">
<label>postcode</label>
<input type="text" class="form-control" formControlName="postcode">
</div>
</div>
</div>
</div>
和控制器:
public myForm: FormGroup;
constructor(private _fb: FormBuilder) { }
ngOnInit() {
this.myForm = this._fb.group({
name: ['', [Validators.required, Validators.minLength(5)]],
addresses: this._fb.array([
this.initAddress(),
])
});
}
initAddress() {
return this._fb.group({
street: ['', Validators.required],
postcode: ['']
});
}
addAddress() {
const control = <FormArray>this.myForm.controls['addresses'];
control.push(this.initAddress());
}
removeAddress(i: number) {
const control = <FormArray>this.myForm.controls['addresses'];
control.removeAt(i);
}
plnkr with working relatively complex forms,which comes from here.
我想看到你的組件/控制器太
你能告訴我們你是如何獲取techListInFB? – silentsod