0

How to get the object of selected option in angular js using ng-repeat? Sample fiddle is here where myself populating the dropdown as,

<select ng-model="id" ng-init="id = '-1'" ng-change="select(option)">
    <option value="-1">--Select--</option>
    <option ng-repeat="option in list" value="{{option.id}}">{{option.name}}</option>
  </select>

If someone chose option with id=1, i want to get the object {id:1, name:'A' in $scope variable.

EDIT: Myself tried with ng-options but filtering is not happening as required based on previous dropdown selection. sample fiddle

2
  • 1
    Use ngOptions instead of manually repeating out the options. From the documentation: ngOptions should be used when the <select> model needs to be bound to a non-string value. Commented Jun 3, 2016 at 5:48
  • Correct.. But if I use ng-options filter won't working properly fiddle: jsfiddle.net/mpsbhat/779pz05o/5 Commented Jun 3, 2016 at 6:28

3 Answers 3

1

Use option object in ng-value={{option}} and whenever u select an option your scope.id will have the desired object. Basically ur ng-model gets populated with selected value.

Sign up to request clarification or add additional context in comments.

Comments

0

Just you have to change your option to this

<option ng-repeat="option in list" value="{{'Id : ' + option.id + ' Name : '  + option.name }}">{{option.name}}</option>

Hope this will work for you and you were looking for the same result

Comments

0

You have 2 option to get as Object.

option 1 :

you can use Filter to get Object

<select ng-model="id" ng-init="id = '-1'" ng-change="select((list|filter:{id:id}))">
    <option value="-1">--Select--</option>
    <option ng-repeat="option in list" value="{{option.id}}">{{option.name}}</option>
 </select>

updated fiddler here

option 2 :

set your value as Object (option)

<select ng-model="id" ng-init="id = '-1'" ng-change="select(option)">
    <option value="-1">--Select--</option>
    <option ng-repeat="option in list" value="{{option}}">{{option.name}}</option>
  </select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.