我在我的angularjs项目上工作。 我有选择下拉元素:
<select ng-model="register.countryId"
ng-options="country.id as country.name for country in register.countries">
<option value = -1 selected>All items</option>
</select>
这个填充下拉元素的对象数组:
$scope.register.countries = [{
id: "1",
name: "India"
}, {
id: "2",
name: "USA"
}, {
id: "3",
name: "UK"
}, {
id: "4",
name: "Nepal"
}];
这是planker。
如何设定此默认值:
<option value = -1 selected>All items</option>
答案 0 :(得分:0)
您必须使用该默认值设置模型,angularjs将显示默认选择的选项。在您的控制器中:
$scope.register.countries = [{
id: "1",
name: "India"
}, {
id: "2",
name: "USA"
}, {
id: "3",
name: "UK"
}, {
id: "4",
name: "Nepal"
}];
// Here you set your model's value
$scope.register.countryId = $scope.register.countries[0].id;// 0 is index and will be replaced by index of default value in the array
// This also works fine. And show selected option is india
$scope.register.countryId = "1";