2016-11-16 39 views
0

我需要選擇的國家名進入鍵名country,但$scope.options將不被認可。我無法選擇國家,更不用說將正確的國家轉變爲關鍵價值。所以有人知道我做錯了什麼,以及在這種情況下可能的解決方案。NG選項需要爲對象值傳遞給數組

任何一個知道如何才能解決這個問題?

\t $scope.addComment = function() { 
 
\t \t $scope.comments.push($scope.dataObject); 
 

 
\t \t $scope.options = [{ 
 
\t \t \t id: 1, 
 
\t \t \t country: "China" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 2, 
 
\t \t \t country: "France" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 3, 
 
\t \t \t country: "Germany" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 4, 
 
\t \t \t country: "Japan" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 5, 
 
\t \t \t country: "Hongary" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 6, 
 
\t \t \t country: "Hong Kong" 
 
\t \t \t }, 
 
\t \t \t { 
 
\t \t \t id: 7, 
 
\t \t \t countrybv : "Italy" 
 
\t \t \t }] 
 

 
\t \t $scope.dataObject = { 
 
\t \t \t name: "", 
 
\t \t \t country: $scope.options[0], 
 
\t \t \t comment: "" 
 
\t \t }; 
 
\t };
<body> 
 
    <td><select class="comment-form comment" ng-model="selected" ng-options="option as option.country for option in options track by option.id"> 
 
    <option value="">Select a country</option> 
 
    </select></td> 
 
</body>

回答

0

試試這個:

<select ng-model="selectedCountry" ng-options="country as country.country for country in countries track by country.id"> 
    <option value="" disabled="" selected>Select Country</option> 
</select> 

<p>country: {{selectedCountry.country}}</p> 
<p>id: {{selectedCountry.id}}</p> 

我已經瑪:

<select ng-model="selected"    
     ng-options="x as x.country for x in options track by x.Id" 
     class="form-control" 
     ng-required="true"> 
    <option value="">-- Choose Country --</option> 
</select> 

For more option see

+0

問題是$ scope.options不會被識別.... –

+0

「track by x.Id」應該是「track by x.id」 – jbrown

+0

我知道,已經閱讀過文檔,但它無法鏈接到ID。這是這樣做的主要問題....也許我忽視了JavaScript的部分東西... –