2015-10-18 90 views
0

爲什麼選擇帶ng-options的標籤可以通過顯示默認選定值正常工作,但如果我使用ng-repeat,則相同不起作用。使用ng-repeat時,選擇值不顯示默認值

這裏是代碼,

<!-- this works--> 

<select class="form-control" ng-model="selectValue" ng-options="option.id as option.name for option in availableOptions"> 
</select> 

<!-- this also works--> 

    <select ng-model="selectValue"> 
     <option ng-repeat="options in availableOptions" value="{{options.id}}" ng-selected="selectValue === options.id">{{options.name}}</option> 
    </select> 

<!-- this doesnt works--> 

<select ng-model="selectValue"> 
    <option ng-repeat="options in availableOptions" ng-value="options.id" ng-selected="selectValue === options.id">{{options.name}}</option> 
</select> 
在app.js

$scope.selectValue="2"; 
    $scope.availableOptions = [ 
     {id: '1', name: 'Option A'}, 
     {id: '2', name: 'Option B'}, 
     {id: '3', name: 'Option C'} 
    ]; 

Though the HTML shows the selected attribute, still its not rendered properly in the view

雖然HTML顯示選定的屬性,它仍然在視圖中不

正確呈現

這裏是猛擊link t他相同

+2

所述第一語法是首選,建議語法;你爲什麼試圖用非首選的語法解決問題? – Claies

+0

雅我知道第一種方法是首選,但只是爲了學習我嘗試了第二種方法。你能弄清楚爲什麼它不工作? – MaheshKumar

回答

0

雖然在頁面上有下拉的正確方法應該使用ng-options,但您可以在下面更改標記以使其與ng-repeat版本一起使用。

標記

<select ng-model="selectValue"> 
    <option ng-repeat="options in availableOptions" value="{{options.id}}"> 
    {{options.name}} 
    </option> 
</select> 

Working Plunkr

+0

但你知道爲什麼它不工作,如果使用ngValue? – MaheshKumar

+0

@MaheSirius其中最糟糕的是......最糟糕的部分是它在將'option'標籤更改爲'' –

+0

雅它非常奇怪。不知道如何弄清楚。 – MaheshKumar