Most select / option solutions for Angular 2 work in such a way that we return the actual content, not the value attribute. However, since I'm still learning Angular 2, I want to get the actual value attribute when the button is clicked. I managed to solve this problem somewhat, but I'm not sure if this is the right approach. The following is an example of how I want it to work:
<select #selectedCategory> <option *ngFor="#category of categories" [value]="category.id">{{category.name}}</option> </select> <button (click)="getValueFromSelect(selectedCategory.value)"> /* This returns the selected category.name, not the value attribute. */
In the above solution, the following HTML is created (note the absence of the value attribute on option ):
<select _ngcontent-oom-3=""> <option _ngcontent-oom-3="">stuff 1</option> <option _ngcontent-oom-3="">stuff 2</option> <option _ngcontent-oom-3="">stuff 3</option> </select>
The solution below really works, however I need ngModel for it to work.
<select [(ngModel)]="selectedCategory"> <option *ngFor="#category of categories" [value]="category.id">{{category.name}}</option> </select> <button (click)="getValueFromSelect(selectedCategory.value)">
What is the right approach to this situation?
Thanks for your suggestions.
source share