0

我有填充然后下拉列表的代码和获取列表中最后一项的javascript变量。现在我要做的就是选择最后一项作为默认值。我错过了什么?

<div class="row">
<div>
    <select ng-init="lastItem" ng-model="congressFilter" ng-options="cc.congressLongName for cc in ccList"></select>
</div>
<div class="grid-style" data-ng-grid="userGrid">
</div>

   ccResource.query(function (data) {
    $scope.ccList.length = 0;
    angular.forEach(data, function (ccData) {
        $scope.ccList.push(ccData);
    })

    //Set default value for dropdownlist?
    $scope.lastItem = $scope.ccList[$scope.ccList.length - 1];
});
4

2 回答 2

3

您只需在控制器中为 congressFilter 分配一个值。

 $scope.congressFilter = 'someVal';

但是,这在一定程度上取决于您的数据的外观。

于 2013-11-11T22:25:51.520 回答
0

它可能对新开发人员有所帮助。需要在选项中添加默认 id 以显示默认项。

下面的代码示例我们在控制器 $scope 中添加 [ $scope.country.stateid = "4" ] 来设置默认值。

    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"
      }];
    });

<body ng-app="myApp">
  <div ng-controller="MyContl">
    <div>
      <select ng-model="country.stateid" ng-options="st.id as st.name for st in country.states">
      </select>
     ID :  {{country.stateid}}
    </div>
  </div>
</body>
于 2014-11-08T07:53:30.923 回答