2016-10-11 116 views
1

我正在嘗試創建一個下拉菜單,並希望用對象預填充它。用對象預填充下拉菜單

<script> 
angular.module("myapp", []) 
    .controller("MyController", function($scope) {  
    $scope.country = { 
     id: "2", 
     name: "USA" 
    }; 

    $scope.countries = [{ 
     id: "1", 
     name: "India" 
    }, { 
     id: "2", 
     name: "USA" 
    }, { 
     id: "3", 
     name: "UK" 
    }, { 
     id: "4", 
     name: "Nepal" 
    }]; 
    }); 
</script> 

<div> 
    Country Name : 
    <select ng-model="country" ng-options="country as country.name for country in countries"></select> 
</div>  

但是國家下拉菜單沒有被預先填入國家對象,我不確定是什麼問題。

的jsfiddle鏈接:http://jsfiddle.net/6qo3vs6c/

+0

你的意思是'ng-options'的默認值嗎?如果這是你正在尋找,請參閱更多[這裏](http://stackoverflow.com/questions/20845210/setting-selected-values-for-ng-options-bound-select-elements)。 – DieuNQ

+0

我想從單獨的對象的默認值...我該怎麼做? –

+0

正確使用你的小提琴。沒有代碼問題。 http://jsfiddle.net/codeandcloud/6qo3vs6c/1/ – naveen

回答

0

刪除country asng-options添加track by country.id

<select ng-model="country" ng-options="country.name for country in countries track by country.id"></select> 

Here是你的。見more

希望這會有所幫助。