使用对象预填充下拉列表

时间:2016-10-11 08:32:37

标签: angularjs

我正在尝试创建一个下拉列表,并希望用对象预填充它。

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

1 个答案:

答案 0 :(得分:0)

删除country as并在track by country.id中添加ng-options

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

Here是你的。请参阅more

希望这有帮助。