2

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.

1
  • 2
    you should use ng-options in this case rather than ng-repeat. option tags can only be bound to strings, so your test1 in this case is not an object, it is literally the string representation of the object. ng-options is designed to overcome this behavior. Commented Jan 20, 2016 at 11:42

2 Answers 2

4

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

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

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.