简体   繁体   English

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

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

I have a select list.我有一个选择列表。

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

I reset the list我重置了列表

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

Nothing happens.什么都不会发生。 AngularJS 1.5.8 AngularJS 1.5.8

There is a bug in 1.4 + of AngularJs. AngularJs 1.4 + 中存在一个错误 Up to at least 1.5.8 in my experience.根据我的经验,至少达到 1.5.8。

Manually reset the list with jQuery.使用 jQuery 手动重置列表。 Ignores angularJS.忽略 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');
  });
};

You need to call this function :你需要调用这个函数:

$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