0

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) {
    $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];
});
1
  • 1
    Shouldn't it be ng-model="lastItem"? Commented Nov 11, 2013 at 22:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.