Search code examples
angularjsdropdownbox

How to assign default value in Dropdownboxlist?


DrodpownissueHi I am developing angularjs application. I tried many ways to set default value for dropdown in Angularjs but I am not able to set default value.

 <select ng-change="getModel(b.ID)" ng-model="b.ID"  id="brand" ng-options="b.MakeName for b in list">
                    <option value="1">-- Select a Make --</option>//Not working
                </select>
                <select  ng-change="getStyle(a.ID)" ng-model="a.ID" ng-options="a.ModelName for a in Modellist" id="make" >
                    <option value="1">-- Select a Model --</option>
                </select>//Not working
                <select ng-change="getallDetails(c.ID)" id="type" ng-model="c.ID" ng-options="c.BodyStayleName for c in ModelStyle">
                    <option value="1">-- Select a Type --</option>
                </select>//Not working

May I know am i missing here anything? Any help would be appreciated. Thank you.


Solution

  • Use ng-init or set the default value to the model variable inside the controller,

      $scope.BrandId = "1";
    

    DEMO

    <!DOCTYPE html>
    <html>
    
    <head>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
      <script>
        angular.module("myapp", [])
          .controller("MyController", function($scope) {
            $scope.register = {};
            $scope.BrandId = "1";
    
            $scope.brands = [{
              id: "1",
              name: "TOYOTA"
            }, {
              id: "2",
              name: "HONDA"
            }, {
              id: "3",
              name: "MARUTI"
            }, {
              id: "4",
              name: "BMW"
            }];
          });
      </script>
    </head>
    
    <body ng-app="myapp">
      <div ng-controller="MyController">
        <div>
          <select ng-init="BrandId=='1'" ng-model="BrandId" ng-options="brand.id as brand.name for brand in brands"></select>
        </div>
      </div>
    </body>
    
    </html>