我有对象$cities = {'name' : 'Bamber', 'id' : 3}
选择列表:
<select ng-selected="selectedCountry == item.id" ng-options="item.id as item.name for item in countries"></select>
selectedCountry
等于2.但ng-selected
对我不起作用
答案 0 :(得分:1)
您必须添加ng-model
AND track by item.id
<select ng-model="country" ng-options="item.id as item.name for item in countries track by item.id"></select>
控制器:
$scope.countries = [
{id: 2, name: 'france'},
{id: 3, name: 'hongrie'}
];
$scope.country = $scope.countries[0];
jsfiddle:http://jsfiddle.net/Fieldset/y414hdkb/
问题是您必须通过索引而不是“真实”国家/地区ID来选择项目,但是在您的控制器中,您可以编写自己的函数来从县ID返回索引
修改强>
使用此代码,您可以从国家/地区ID中找到索引:)
var defaultCountryId = 2;
var defaultCountryIndex = $scope.countries.map(function(country){return country.id;}).indexOf(defaultCountryId);
$scope.country = $scope.countries[defaultCountryIndex];