AngularJs Get the value of the selected option

那年仲夏 提交于 2019-12-08 14:52:40

问题


How can i get the value of the selected option on the ng-change "ReadData" Template is an array of Title, Id, and body

<select class="form-control" id="inputEmail" 
    ng-change="ReadData(Template.Title)" 
    ng-model="email.Template" name="inputEmail" 
    ng-options="b.Id as b.Title for b in Template">
</select>

回答1:


This might work:

ng-change="ReadData(Template[$index].Title)"




回答2:


Since your select is bound (by means of ngModel) to email.Template, you can access it's value from there. E.g.:

<select ... ng-change="ReadData(email.Template)" ...

Note though that, as a result of how you define ngOptions, email.Template will be bound to the teplate's id. If you want it to be bound to the whole template object itself (so you can use email.Template.Title for example), change your code like this:

<select ... ng-options="b as b.Title for b in Template" 
        ... ng-change="ReadData(email.Template.Title)" ...



回答3:


You don't need to use ng-change to get selected data, in <select> your ng-model bind will hold selected option. You can just get that value in your ng-change;

<select class="form-control" id="inputEmail"
    ng-change="onOptionChange()" 
    ng-model="email.Template" name="inputEmail" 
    ng-options="b.Id as b.Title for b in Template">
</select>

Here is a JSFIDDLE


EDIT: Also if you want to entire selected Template object, change you ng-options to this:

ng-options="b.Title for b in Template"

This will assign b to email.Template, not just b.Id.



来源:https://stackoverflow.com/questions/22930599/angularjs-get-the-value-of-the-selected-option

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