This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
docs(ngOptions): explain using 'select as' and 'track by' together #13007
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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' } | ||
|
@@ -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` | ||
* value. In the first example, the `ngModel` value is `item[x]` and the **`track by`** expression evaluates to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. item[x] --> items[x] |
||
* expression evaluates to `item.subItem.id`. As a result, the selected element is never found and the `<select>` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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 ?