我已經做了一些研究,我很驚訝地發現它並不像應該那樣直截了當。如何使用formBuilder綁定angular2中的選擇下拉列表?
我知道有一些使用ngModel的方法。像Bind and fetch dropdown list value in typescript and Angular2等。但我想能夠輕鬆地將我選擇的選項綁定到我的formControlName var。
這是我曾嘗試:
.HTML
<form id="myForm" name="father" [formGroup]="fatherForm" (ngSubmit)="createFather()">
<div class="row">
<select formControlName="pref" id="f">
<option value=null disabled selected>Prefijo</option>
<option *ngFor="let item of prefs" [ngValue]="hola">{{item.name}}</option>
</select>
</div>
</form>
.TS
fatherForm: FormGroup;
this.fatherForm = this.formBuilder.group({
pref: ['AA']
});
this.prefs=[
{name: "AA"},
{name: "BB"}
];
默認值的作品。但是當我選擇BB時,默認值仍然被選中。 同當Bind Select List with FormBuilder Angular 2
我在做什麼錯誤的默認值是「」
建議這個方案是由哈利 - 廣寧發生什麼呢?
注意:當然,還有其他輸入形式,爲了簡單起見,忽略它們。
編輯
我試圖用這個plunkr完全相同的例子,它也不管用。 http://plnkr.co/edit/Rf9QSSEuCepsqpFc3isB?p=preview 發送表單後,表明該值沒有被觸及。
你檢查此[鏈接](http://stackoverflow.com/questions/39689049/drop-down-list-in-angular-2-model-driven-form) –