我有一系列对象,如下所示,我想使用ng-options在select标签中显示。
表示默认选项
在控制器中
$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"
没有选择默认对象
你可以建议出现什么问题吗?先谢谢, 普拉萨德。
答案 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
如果您更喜欢按国家代码选择(对我来说似乎更干净),还有另一个例子: