我有一个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) –