如何在 Dropdownboxlist 中分配默认值?

How to assign default value in Dropdownboxlist?

您好,我正在开发 angularjs 应用程序。我尝试了很多方法来为 Angularjs 中的下拉菜单设置默认值,但我无法设置默认值。

 <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

我可以知道我在这里遗漏了什么吗?任何帮助,将不胜感激。谢谢。

使用ng-init或者控制器内部的model变量设置默认值,

  $scope.BrandId = "1";

演示

<!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>