简体   繁体   中英

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

There is a bug in 1.4 + of AngularJs. Up to at least 1.5.8 in my experience.

Manually reset the list with jQuery. Ignores 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 = "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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