如何在Dropdownboxlist中指定默认值?

时间:2017-04-17 13:36:18

标签: angularjs dropdownbox

Drodpownissue您好我正在开发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

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

1 个答案:

答案 0 :(得分:1)

使用 ng-init 或将默认值设置为控制器内的模型变量,

  $scope.BrandId = "1";

<强>样本

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;