Disha
Disha

Reputation: 832

Angularjs select ng-model binding for object

I want to have a select for array of objects.example But somehow , I am not able to access properties of selected object.

js---

 $scope.test1={};
 $scope.test = [{'name':'test1'},{'name':'test2'},{'name':'test3'}];

html--

<select style="width:100px;height:25px;" ng-model="test1">
   <option  ng-repeat="attribute in test" value="{{attribute}}">{{attribute['name']}}</option>
</select>
{{test1}}
{{test1.name}}

here , test1.name comes blank.

Upvotes: 2

Views: 5393

Answers (2)

RIYAJ KHAN
RIYAJ KHAN

Reputation: 15292

Do it using ngOpions this way.It gives proper controll than ng-repeat

<select style="width:100px;height:25px;" ng-model="test1"  
     ng-options="attribute.name  for  attribute in test">

Here is the Plunker

Upvotes: 4

Hristo Enev
Hristo Enev

Reputation: 2541

This is because the select value is interpreted as string. Not as object. And of course strings don't have name property. You can use ng-options if you want your values to contain the whole object. Read the documentation here.

Upvotes: 1

Related Questions