4

What is the purpose of the trackexpr (track by) in ng-options when using Angular 1.3?

In Angular 1.2, this expression changed the value="" expressions on the generated options to match the result of trackexpr on each item in the collection. This is no longer the case in Angular 1.3, per this jsfiddle: http://jsfiddle.net/3fzkym3m/. Now the options just have incremental values.

In Angular 1.2, the generated options list is:

<select ng-options="item.text for item in data track by item.value" ng-model="selectedItem" class="ng-pristine ng-valid">
    <option value="" class=""> - Select item - </option>
    <option value="4">item1</option>
    <option value="12">item2</option>
    <option value="11">item3</option>
    <option value="19">item4</option>
    <option value="17">item5</option>
    <option value="26">item6</option>
</select>

In Angular 1.3, the generated options list is:

<select ng-options="item.text for item in data track by item.value" ng-model="selectedItem" class="ng-pristine ng-untouched ng-valid">
    <option value="" class=""> - Select item - </option>
    <option value="0">item1</option>
    <option value="1">item2</option>
    <option value="2">item3</option>
    <option value="3">item4</option>
    <option value="4">item5</option>
    <option value="5">item6</option>
</select>

Is Angular still using track by to match objects between the source collection and the selected item? Is there still a performance benefit to doing this? Why did this change between Angular 1.2 and 1.3?

Claies
  • 21,481
  • 3
  • 49
  • 75
Josh G
  • 13,566
  • 7
  • 55
  • 72

1 Answers1

1

Based on this issue on AngularJS's GitHub repository, it appears that this was an unintended side effect of changes made in Angular 1.3. https://github.com/angular/angular.js/issues/9592. Per the discussion there it sounds like they intend to revert the behavior to the way it was in Angular 1.2.

Josh G
  • 13,566
  • 7
  • 55
  • 72