0

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.

Niranjan Godbole
  • 2,135
  • 7
  • 43
  • 90
  • you should init the model here `b.ID` – Hadi J Apr 17 '17 at 13:38
  • Possible duplicate of [how to use ng-option to set default value of select element](http://stackoverflow.com/questions/17329495/how-to-use-ng-option-to-set-default-value-of-select-element) – Castro Roy Apr 17 '17 at 14:19
  • Have you google for it? I've just found a big list of other SO questions/answers than can help you with your issue [Why does AngularJS include an empty option in select?](http://stackoverflow.com/questions/12654631/why-does-angularjs-include-an-empty-option-in-select) [ng-options how to set first select always blank](http://stackoverflow.com/questions/19182961/ng-options-how-to-set-first-select-always-blank) [How to have a default option in Angular.js select box](http://stackoverflow.com/questions/18194255/how-to-have-a-default-option-in-angular-js-select-box) – Castro Roy Apr 17 '17 at 14:22

1 Answers1

1

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>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396