Skip to content

Commit b6673f4

Browse files
committed
docs(select): Clarify documentation for ngOptions
ngOptions introduced `track by` in c32a859. Using `track by` puts constraints on the value you can use in the interpolation expression in ngOptions. This patch documents this. Closes angular#6564
1 parent a8fe2cc commit b6673f4

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/ng/directive/select.js

+13
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ var ngOptionsMinErr = minErr('ngOptions');
7070
* * `trackexpr`: Used when working with an array of objects. The result of this expression will be
7171
* used to identify the objects in the array. The `trackexpr` will most likely refer to the
7272
* `value` variable (e.g. `value.propertyName`).
73+
* <div class="alert alert-warning>
74+
* Do not use `track by` when you have a complex `select` expression that is different from `value`.
75+
* Use `track by` if you want to assign complex objects to the model. Use a complex `select`
76+
* expression like `item.value as item.label for item in items` if your items are objects but your
77+
* model values correspond to properties of these objects.
78+
* See an example [in this jsfiddle](http://jsfiddle.net/0vpsv1wa/1/).
79+
* </div>
7380
*
7481
* @example
7582
<example module="selectExample">
@@ -338,6 +345,12 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
338345
// - optionGroupsCache[?][0] is the parent: either the SELECT or OPTGROUP element
339346
optionGroupsCache = [[{element: selectElement, label:''}]];
340347

348+
if (track && match[2] && valueName !== match[1]) {
349+
throw ngOptionsMinErr('trackSelect',
350+
"Do not use 'track by' when your select ('{0}') is different from your value ('{1}')",
351+
match[1], valueName);
352+
}
353+
341354
if (nullOption) {
342355
// compile the element since there might be bindings in it
343356
$compile(nullOption)(scope);

0 commit comments

Comments
 (0)