如何在 AngularJS 中重置 select 列表并进行跟踪?

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

我有一个 select 列表。

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

没有任何反应。 AngularJS1.5.8

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');
  });
};

您需要调用此函数:

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