Interlated
Interlated

Reputation: 5946

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

Upvotes: 1

Views: 2010

Answers (2)

Interlated
Interlated

Reputation: 5946

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

Upvotes: 0

Jigar7521
Jigar7521

Reputation: 1579

You need to call this function :

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

Upvotes: 0

Related Questions