0
我目前在Angular2组件中创建了一些功能,其中对象数组循环以创建表单选择框的选项。Angular2选择框将对象作为字符串传递
我有这个工作,当选择框选项更改时,我调用一个处理函数,我打算通过选择的选项。
我现在遇到的问题是我无法让对象从视图传递到控制器。该对象被传递,虽然作为一个字符串:
[object object]
这里是我的代码:
观点:
<div class='searchbar-wrapper'>
<h5>MANUFACTURER</h5>
<select class='manufacturers' #m [(ngModel)]="selectedManufacturer" (change)="changeManufacturer(m.value)">
<option>-- please select --</option>
<option *ngFor="let manufacturer of manufacturers" [ngValue]="manufacturer">{{manufacturer.name}}</option>
</select>
</div>
控制器:
changeManufacturer(manufacturer) {
this.productsLoaded = false;
this.selectedManufacturer = manufacturer;
this.productPayload.manufacturer = manufacturer.id;
this.getProducts();
}
正如你所看到的,我试图将对象传递给控制器,但这不起作用。任何人都可以建议为什么它像字符串一样通过?
Thankss
我会建议你使用manufacturer.id作为ngValue然后您可以使用该ID从班级中的manfacturers list/Array设置正确的SelectedManugacturer。这将是最佳的。 – Jeet
感谢您的回复。我已经尝试过,但我仍然把它作为一个字符串:“13:13” – devoncrazylegs