ng-options需要作为对象值传递给数组

时间:2016-11-16 15:54:47

标签: angularjs

我需要将所选国家/地区名称传递给密钥名称country,但无法识别$scope.options。我不能选择国家,更不用说将正确的国家传递给关键价值。那么有谁知道我做错了什么,以及在这种情况下解决方案可能是什么。

任何人都知道如何解决这个问题?



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

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

		$scope.dataObject = {
			name: "", 
			country: $scope.options[0], 
			comment: ""
		};
	};

<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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

试试这个:

<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

答案 1 :(得分:0)

in html:

<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>

我在plnkr中做了这个: https://plnkr.co/edit/406tgmG5L4s5SFEeUlF3?p=info

祝你好运!