2016-11-16 35 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的部分东西... –