繁体   English   中英

如何在 AngularJS 中重置选择列表并跟踪?

[英]How do I reset a select list in AngularJS and track by?

我有一个选择列表。

<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 1.4 + 中存在一个错误 根据我的经验,至少达到 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');
  });
};

你需要调用这个函数:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM