mpsbhat
mpsbhat

Reputation: 2773

angularjs ng-repeat selected option

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

Upvotes: 0

Views: 1170

Answers (3)

Aravinthan K
Aravinthan K

Reputation: 1861

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>

Upvotes: 0

Usman Khan
Usman Khan

Reputation: 359

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

Upvotes: 0

adiinstack
adiinstack

Reputation: 96

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.

Upvotes: 1

Related Questions