Search code examples
angularjsangularjs-track-by

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


Solution

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