2016-01-20 95 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.