Set default value for dropdownlist using angularjs?

后端 未结 2 604
臣服心动
臣服心动 2021-01-17 04:23

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 .W

相关标签:
2条回答
  • 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.

    0 讨论(0)
  • 2021-01-17 05:03

    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>
    
    0 讨论(0)
提交回复
热议问题