ng-options缺省选项对象未使用对象数组进行选择

时间:2015-08-25 12:22:53

标签: angularjs angularjs-ng-options

我有一系列对象,如下所示,我想使用ng-options在select标签中显示。

  1. 我必须将国家/地区名称显示为显示名称
  2. 我有强制性商店选择'对象'在模型选项
  3. 表示默认选项

    在控制器中

    $scope.selectedCountry = {"US":"United States"}
    $scope.countries: [
       {"name":"United States", code: "US"},
       {"name":"Canada", code: "US"},
       {"name":"Afghanistan", code: "US"},
       {"name":"Albania", code: "US"},
       {"name":"Algeria", code: "US"},
       {"name":"American Samoa", code: "US"},
          .....
       ];
    

    在html页面中:

    ng-model="selectedCountry" ng-options="country as country.name for country in countries track by country"
    

    没有选择默认对象

    你可以建议出现什么问题吗?

    先谢谢, 普拉萨德。

1 个答案:

答案 0 :(得分:1)

您应该像这样设置默认值:

$scope.countries= [
   {"name":"United States", code: "US"},
   {"name":"Canada", code: "C"},
   {"name":"Afghanistan", code: "A"},
   {"name":"Albania", code: "AL"},
   {"name":"Algeria", code: "AG"},
   {"name":"American Samoa", code: "AS"}
];
$scope.selectedCountry = $scope.countries[1]; //Select Canada

您还需要删除曲目。

这里有一个有效的例子:

http://plnkr.co/edit/kkq0Fy4NCfBIa0Ioy8En?p=preview

如果您更喜欢按国家代码选择(对我来说似乎更干净),还有另一个例子:

http://plnkr.co/edit/TaCcpgtMLYFvzQqcdGKS?p=preview