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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(select): throw for selectAs and trackBy
trackBy and selectAs have never worked together, and are fundamentally incompatible since model changes cannot deterministically be reflected back to the view. This change throws an error to help developers better understand this scenario.
- Loading branch information
1 parent
aad6095
commit abdaab7
Showing
3 changed files
with
55 additions
and
170 deletions.
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
@ngdoc error | ||
@name ngOptions:trkslct | ||
@fullName Comprehension expression cannot contain both `select as` and `track by` expressions. | ||
@description | ||
|
||
This error occurs when 'ngOptions' is passed a comprehension expression that contains both a | ||
`select as` expression and a `track by` expression. These two expressions are fundamentally | ||
incompatible. | ||
|
||
* Example of bad expression: `<select ng-options="item.subItem as item.label for item in values track by item.id" ng-model="selected">` | ||
`values: [{id: 1, label: 'aLabel', subItem: {name: 'aSubItem'}}, {id: 2, label: 'bLabel', subItem: {name: 'bSubItem'}}]`, | ||
`$scope.selected = {name: 'aSubItem'};` | ||
* track by is always applied to `value`, with purpose to preserve the selection, | ||
(to `item` in this case) | ||
* To calculate whether an item is selected, `ngOptions` does the following: | ||
1. apply `track by` to the values 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}`. | ||
|
||
Here's an example of how to make this example work by using `track by` without `select as`: | ||
|
||
``` | ||
<select ng-model="selected" ng-options="item.label for item in values track by item.id"> | ||
``` | ||
|
||
Note: This would store the whole `item` as the model to `scope.selected` instead of `item.subItem`. | ||
|
||
For more information on valid expression syntax, see 'ngOptions' in {@link ng.directive:select select} directive docs. |
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
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