Set default value for dropdownlist using angularjs?

后端 未结 2 606
臣服心动
臣服心动 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条回答
  •  广开言路
    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"
          }];
        });
    
    
      
    ID : {{country.stateid}}

提交回复
热议问题