我有一個Angular 2應用程序正在處理客戶和春季休息後端。客戶對象有一個客戶類型,這也是一個對象,我有客戶表單上的下拉菜單,因此對象被存儲爲值,但我無法弄清楚如何選擇正確的客戶類型現有客戶被加載到表單中。Angular2選擇對象的選項
<select class="form-control" required [(ngModel)]="customer.customerType" >
<option *ngFor="let ct of customerTypes" [ngValue]="ct">{{ct.customerType}}</option>
</select>
在如果客戶已經有一個客戶類型上面的代碼,然後在下拉不會選擇任何值。我記得有與angular1這是解決使用ngOptions同樣的問題:
<select ng-model="customer.customerType"
ng-options="customerType.customerType for customerType in customerTypes
track by customerType.customerType" >
</select>
所以,我的問題是,如何複製Angular1解決了角2
只需將默認值分配給'customer.customerType'即可。 'ngValue'和'customer.customerType'需要指向同一個對象實例,而不僅僅指向兩個具有相同內容的對象。 –
我意識到它需要相同的對象實例,而不是具有相同內容的對象,但我想知道是否有一種方法可以通過類似於java中的比較器的方式傳遞select,它會使用以評估對象實例是否相同。我已經結束了在其他調用中返回的實例,以及來自select選項的等價對象,這感覺真的很笨重。 – Drew
[將選擇元素綁定到Angular 2中的對象]可能的重複(https://stackoverflow.com/questions/35945001/binding-select-element-to-object-in-angular-2) –