html - AngularJS select not showing selected value
问题描述
Controller
$scope.industries= [
{
id: 1,
name: "Name1",
}, {
id: 2,
name: "Name2"
}
];
When assign
$scope.filling_type = 2
in controller,I want to select "Name2" option in the select.
View
<select ng-model="filling_type">
<option ng-repeat="industry in industries"
ng-value="industry.name">{{industry.name}}</option>
</select>
In this case no option selected.
解决方案
I'd recommend getting used to ng-options
in the select element instead of ng-repeat
ing in the option element because of increased flexibility and performance. https://docs.angularjs.org/api/ng/directive/select#choosing-between-ngrepeat-and-ngoptions-
var app = angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.industries= [
{
id: 1,
name: "Name1",
}, {
id: 2,
name: "Name2"
}
];
$scope.filling_type = 2; // set default option here, using 2 from OP
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<select
ng-model="filling_type"
ng-options="i.id as i.name for i in industries">
</select>
<p>Filling Type: {{filling_type}}</p>
</body>
推荐阅读
- java - 使用 Jackson 反转 JSON 漂亮打印
- sql-server - 确定 Azure 移动服务表是否为空的最快客户端方法是什么?
- javascript - javascript - 当字符串以空格字符结尾时,split(" ") 方法在数组的最后一个索引处给出什么?
- android - 如何为firebase数据库子声明字符串值
- python - 将系列索引中的值添加到 Pandas DataFrame 中相等值的行
- sql-server - 是否生成脚本(架构和数据)会影响服务器/数据库的性能
- javascript - 从日期编号创建一系列日期名称?
- jquery - 从 localhost 的第二个窗口登录时,LocalStorage 中的数据会被覆盖
- php - PHP比较不起作用
- c++ - SDL_SetRenderTarget 不设置目标