我試圖添加嵌套下拉。如果我們點擊「添加城市」,它會創建另一個下拉菜單。我沒有下拉式成功。請參見下文角4嵌套下拉
..component.html
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<select formControlName="cities">
<option *ngFor="let city of myCities; let i=index" [value]='itemname' >
{{ city }}
</option>
</select>
<br>
<button>Submit</button>
<button (click)="addCity()">Add City</button>
<button (click)="remove(i)">Remove</button>
</form>
component.ts
form = new FormGroup({
cities: new FormControl([null]),
});
myCities = ['Dhaka','Dubai','Munich'];
get cities(): FormArray { return this.form.get('cities') as FormArray; }
addCity() { this.form.get('cities').push(new FormControl())}
remove(index){ this.form.get('cities').removeAt(index) }
顯示錯誤波紋管:
E:/angular/nestedForm2/src/app/order-form/order-form.component.ts (21,39): Property 'push' does not exist on type 'AbstractControl'.
E:/angular/nestedForm2/src/app/order-form/order-form.component.ts (23,42): Property 'removeAt' does not exist on type 'AbstractControl'.
我已經TR以不同的方式,但仍然沒有找到任何解決方案。能否請你幫忙?
謝謝拉扎爾。你能告訴我如何施展? – Tanvir
我在回答中提供了一段代碼片段。正如我在那裏展示的那樣,你使用'as'來投射。 –
我做了這個addCity(){(this.form.get('cities')as FormArray).push(new FormControl())}&remove(index){(this.form.get('cities')as FormArray).removeAt(index)}。但仍然發現錯誤「ERROR TypeError:this.form.get(...)。push不是函數」 – Tanvir