Selecting an option only Display Id not Name in angular 4

谁说胖子不能爱 提交于 2019-12-02 04:50:54

create a new variable in ts files name - PackingItem = {};

in html file change value={{item.PackingTypeID}} to [value]="item"

then in addItem() method check console.log(this.PackingItem);

Let me know if you have any doubt.

Whatever is in the value for your option is what you will get on select.

Right now, you're setting it to just the ID, but you can set the value to the whole object if you want.

<option *ngFor="let item of products" id="{{item.PackingTypeID}}" [value]="item">{{item.PackingtypeName}}</option>

This will give you the whole item to play with on select instead of a single value.

Your onSelect will now look like:

onSelect(item: PackingType) {
  alert(`ID: ${item.PackingTypeID} - Name: ${item.PackingtypeName}`);
}

(Assuming your DTO is a PackingType type - I don't see any reference to the exact type you're using.. but I guess any would do. Either way, the object will be the actual item object).

Edit: added full option line, including mentioned fix for [input] of the value, and example of updated onSelect method. Don't phone and stack, peeps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!