2016-01-20 134 views
0

我想要选择一个对象数组。 example但不知何故,我无法访问所选对象的属性。Angularjs为对象选择ng-model绑定

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}} 

这里,test1.name自带空白。

+2

你应该在这种情况下使用'NG-options'而不是'NG-repeat' 。 'option'标签只能绑定到字符串,所以在这种情况下'test1'不是一个对象,它实际上是对象的字符串表示。 'ng-options'旨在克服这种行为。 – Claies

回答

2

它使用ngOpions做到这一点way.It给出比ng-repeat

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

这里适当的控制研究是Plunker

1

这是因为selectvalue被解释为字符串。不作为对象。当然,字符串没有name属性。如果您希望您的值包含整个对象,则可以使用ng-optionsRead the documentation here.