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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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