【问题标题】:How do I reset a select list in AngularJS and track by?如何重置 AngularJS 中的选择列表并跟踪?
【发布时间】:2016-10-16 06:10:21
【问题描述】:

我有一个选择列表。

<select id="search.month" 
  ng-model="search.month" 
  class="form-control" 
  ng-options="item as item.name for item in months track by item.id">
 </select>

我重置了列表

$scope.reset = function () {
  $scope.search = {reportType: 0, month: 0, year: 0 };
};

什么都没有发生。 AngularJS 1.5.8

【问题讨论】:

    标签: angularjs angularjs-track-by


    【解决方案1】:

    你需要调用这个函数:

    $scope.resetDropDown = function() {
     $scope.temp = $scope.search.month; // Before reset select box, get selected value
        $scope.search.month = "";
    }
    

    【讨论】:

    • 似乎不起作用。 $scope.temp 从 0 开始,我已经在 $scope.search = {reportType: 0, month: 0, year: 0 }; 中将 $scope.search.month 设置为 0
    【解决方案2】:

    在 AngularJs 的 1.4 + 中有一个 bug。根据我的经验,至少达到 1.5.8。

    使用 jQuery 手动重置列表。忽略 angularJS。

    $scope.reset = function () {
      $scope.search = {reportType: 0, month: 0, year: 0 };
      // Manually reset.
      // https://github.com/angular/angular.js/issues/14892
      $('#search\\.month').find('> option').each(function() {
         $(this).removeAttr('selected');
      });
    
      $('#search\\.year').find('> option').each(function() {
        $(this).removeAttr('selected');
      });
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-18
      • 2022-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多