需求: 我要把一組一組的FormGroup塞入FormArray中,編譯時都沒有問題,但在進行AOT編譯的時候卻出現了以下的錯誤訊息:
Argument of type ‘AbstractControl’ is not assignable to parameter
of type ‘FormGroup’
附上原始碼:
↓↓↓ .ts檔
ngOnInit() {
const passArray = new FormArray([]);
const passCondition = this.editService.getPassConditionArray();
for (const condition of passCondition) {
console.log(condition);
passArray.push(
new FormGroup({
condition: new FormGroup({
diveAttribute: new FormControl(condition.condition['diveAttribute']),
operator: new FormControl(condition.condition['operator']),
value: new FormControl(condition.condition['value'])
}),
logical: new FormControl(condition['logical'])
})
);
}
getConditionArrayForm(form: FormGroup) {
return form.controls; //直接在template寫.controls會報錯,所以迂迴的寫了這一段
}
↓↓↓ .html檔
<form [formGroup]="passForm">
<div formArrayName="passArray">
<div [formGroupName]="i" *ngFor="let condition of getConditionArrayForm(passForm.get('passArray'));let i = index">
//***報錯在上面那行***
<div *ngIf="i !== 0">
<div class="col-2">
<select formControlName="logical" class="form-control" >
<option value="and">且</option>
<option value="or">或</option>
</select>
</div>
</div>
<div class="form-row" formGroupName="condition">
<div class="col-5">
<select formControlName="diveAttribute" class="form-control">
<option *ngFor="let item of diveItems;" [value]="item.dataValue">
{{item.viewValue}}
</option>
</select>
</div>
<div class="col-2">
<select formControlName="operator" class="form-control">
<option *ngFor="let operator of operators" [value]="operator">
{{ operator }}
</option>
</select>
</div>
<div class="col-4">
<input type="text" formControlName="value" class="form-control" placeholder="請輸入數值">
</div>
</div>
</div>
</div>
</form>