0

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>
Cem Özer
  • 1,263
  • 11
  • 19
user3507347
  • 41
  • 1
  • 1
  • 2

3 Answers3

1

This might work:

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

Alagarasan M
  • 907
  • 8
  • 16
0

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)" ...
gkalpak
  • 47,844
  • 8
  • 105
  • 118
  • email and template are not in the same scope – user3507347 Apr 08 '14 at 08:06
  • I don't really get your point. You are referencing both from your ` – gkalpak Apr 08 '14 at 09:44
0

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.

Cem Özer
  • 1,263
  • 11
  • 19
  • i know but i have to get the id too on your jsfiddle i don't have Selected: and the selected item's title but the id – user3507347 Apr 08 '14 at 08:02
  • I don't think this can be done, since ng-options has its own isolate scope, you can't get data like that if it's not exposed by directive itself. You must use entire object in ng-model and on ng-change make proper assignments. – Cem Özer Apr 08 '14 at 08:14