admin管理员组文章数量:1391964
I have code that populates then dropdownlist and the javascript variable that gets the last item in the list. Now all I want to do is select that last item as the default .What am I missing ?
<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) {
$scopeList.length = 0;
angular.forEach(data, function (ccData) {
$scopeList.push(ccData);
})
//Set default value for dropdownlist?
$scope.lastItem = $scopeList[$scopeList.length - 1];
});
I have code that populates then dropdownlist and the javascript variable that gets the last item in the list. Now all I want to do is select that last item as the default .What am I missing ?
<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) {
$scopeList.length = 0;
angular.forEach(data, function (ccData) {
$scopeList.push(ccData);
})
//Set default value for dropdownlist?
$scope.lastItem = $scopeList[$scopeList.length - 1];
});
Share
Improve this question
asked Nov 11, 2013 at 22:11
punkouterpunkouter
5,36617 gold badges78 silver badges123 bronze badges
1
-
1
Shouldn't it be
ng-model="lastItem"
? – elclanrs Commented Nov 11, 2013 at 22:13
2 Answers
Reset to default 3You simply need to asign a value to congressFilter in your controller.
$scope.congressFilter = 'someVal';
It depends a little on how your data looks however.
It might help to new developers. need to add default id for display default item in option.
The below code sample we add [ $scope.country.stateid = "4" ] in controller $scope to set the default.
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>
本文标签: javascriptSet default value for dropdownlist using angularjsStack Overflow
版权声明:本文标题:javascript - Set default value for dropdownlist using angularjs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744699448a2620477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论