使用 angularjs 设置下拉列表的默认值
Set default value for dropdownlist using angularjs?
我有填充然后下拉列表的代码和获取列表中最后一项的 javascript 变量。现在我要做的就是选择最后一项作为默认值.我错过了什么?
<div class="row">
<div>
<select ng-init="lastItem" ng-model="congressFilter" ng-options="cc.congressLongName for cc in ccList"></select>
</div>
<div class="grid-style" data-ng-grid="userGrid">
</div>
ccResource.query(function (data) {
$scope.ccList.length = 0;
angular.forEach(data, function (ccData) {
$scope.ccList.push(ccData);
})
//Set default value for dropdownlist?
$scope.lastItem = $scope.ccList[$scope.ccList.length - 1];
});
您只需要在控制器中为 congressFilter 分配一个值即可。
$scope.congressFilter = 'someVal';
但是,这有点取决于您的数据外观。
它可能会对新开发人员有所帮助。 需要为选项添加默认 ID。
下面的代码示例我们在控制器中添加 [ $scope.country.stateid = "4" ] $scope以设置默认值。
var aap = angular.module("myApp", []);
aap.controller("MyContl", function($scope) {
$scope.country = {};
$scope.country.stateid = "4";
$scope.country.states = [{
id: "1",
name: "UP"
}, {
id: "4",
name: "Delhi"
}];
});
<body ng-app="myApp">
<div ng-controller="MyContl">
<div>
<select ng-model="country.stateid" ng-options="st.id as st.name for st in country.states">
</select>
ID : {{country.stateid}}
</div>
</div>
</body>
相关文章:
- KendoUI网格行过滤器,带有布尔值下拉列表
- 根据两个下拉列表的默认值禁用按钮
- 如何获取下拉列表的默认值
- 如何在第二个下拉列表中更改设置的默认值
- 我无法将下拉列表的默认值设置为最后一项
- 使用 angularjs 设置下拉列表的默认值
- ASP 剑道下拉列表默认值未使用定义的模板
- 在多选下拉列表中选择默认值
- 在 js 中循环以设置下拉列表的默认值.在 IE8 中不起作用
- 更新 HTML.隐藏对于具有选定值形式的值下拉列表的值
- 在页面加载时将动态值设置为下拉列表中的默认值
- 如何在不使用内联 javascript 的情况下修改选择下拉列表的默认值
- 如何在本地存储中设置选择下拉列表的值并检索它,以及将值设置为默认值,除非更改
- 如何将默认值设置为下拉列表
- 未选择下拉列表中的默认值
- 在选择下拉列表中的值时向某些控件添加了默认选项
- 在HTML表中搜索-组合多个值(下拉列表、文本字段)
- 默认文本下拉列表jquery
- 如何默认Html.下拉列表到无选择
- 从隐藏值下拉列表中选择默认值