Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

docs(ngOptions): explain using 'select as' and 'track by' together #13007

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,13 @@ var ngOptionsMinErr = minErr('ngOptions');
* ### `select` **`as`** and **`track by`**
*
* <div class="alert alert-warning">
* Do not use `select` **`as`** and **`track by`** in the same expression. They are not designed to work together.
* Be careful when using `select` **`as`** and **`track by`** in the same expression.
* </div>
*
* Consider the following example:
*
* ```html
* <select ng-options="item.subItem as item.label for item in values track by item.id" ng-model="selected"></select>
* ```
* Given this array of items on the $scope:
*
* ```js
* $scope.values = [{
* $scope.items = [{
* id: 1,
* label: 'aLabel',
* subItem: { name: 'aSubItem' }
Expand All @@ -77,20 +73,32 @@ var ngOptionsMinErr = minErr('ngOptions');
* label: 'bLabel',
* subItem: { name: 'bSubItem' }
* }];
* ```
*
* $scope.selected = { name: 'aSubItem' };
* This will work:
*
* ```html
* <select ng-options="item as item.label for item in items track by item.id" ng-model="selected"></select>
* ```
* ```js
* $scope.selected = $scope.items[0];
* ```
*
* but this will not work:
*
* With the purpose of preserving the selection, the **`track by`** expression is always applied to the element
* of the data source (to `item` in this example). To calculate whether an element is selected, we do the
* following:
* ```html
* <select ng-options="item.subItem as item.label for item in items track by item.id" ng-model="selected"></select>
* ```
* ```js
* $scope.selected = $scope.items[0].subItem;
* ```
*
* 1. Apply **`track by`** to the elements in the array. In the example: `[1, 2]`
* 2. Apply **`track by`** to the already selected value in `ngModel`.
* In the example: this is not possible as **`track by`** refers to `item.id`, but the selected
* value from `ngModel` is `{name: 'aSubItem'}`, so the **`track by`** expression is applied to
* a wrong object, the selected element can't be found, `<select>` is always reset to the "not
* selected" option.
* In both examples the **`track by`** expression is applied successfully to each `item` in the `items` array.
* Because the selected option has been set in code, the **`track by`** expression is also applied to the `ngModel`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"has been set in code" doesn't really tell me anything. How about "has been set from the scope"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it to "has been set programmatically in the controller". How about that ?

* value. In the first example, the `ngModel` value is `item[x]` and the **`track by`** expression evaluates to
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item[x] --> items[x]

* `item[x].id` with no issue. In the second example, the `ngModel` value is `item.subItem` and the **`track by`**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item[x] --> items[x]
item.subItem --> items[x].subItem

* expression evaluates to `item.subItem.id`. As a result, the selected element is never found and the `<select>`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

item.subItem.id --> items[x].subItem.id

* always appears to be unselected.
*
*
* @param {string} ngModel Assignable angular expression to data-bind to.
Expand Down