Skip to content

Commit a8e03b3

Browse files
committed
docs(ngOptions): add info about preselecting complex models
Closes angular#12966
1 parent 6f3e26c commit a8e03b3

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/ng/directive/ngOptions.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,28 @@ var ngOptionsMinErr = minErr('ngOptions');
3333
*
3434
* ## Complex Models (objects or collections)
3535
*
36-
* **Note:** By default, `ngModel` watches the model by reference, not value. This is important when
37-
* binding any input directive to a model that is an object or a collection.
36+
* By default, `ngModel` watches the model by reference, not value. This is important to know when
37+
* binding the select to a model that is an object or a collection.
3838
*
39-
* Since this is a common situation for `ngOptions` the directive additionally watches the model using
40-
* `$watchCollection` when the select has the `multiple` attribute or when there is a `track by` clause in
41-
* the options expression. This allows ngOptions to trigger a re-rendering of the options even if the actual
42-
* object/collection has not changed identity but only a property on the object or an item in the collection
43-
* changes.
39+
* One issue occurs if you want to preselect an option. For example, if you set
40+
* the model to an object that is equal to an object in your collection, `ngOptions` won't be able to set the selection,
41+
* because the objects are not identical. So by default, you should always reference the item in your collection
42+
* for preselections, e.g.: `$scope.selected = $scope.collection[3]`.
43+
*
44+
* Another solution is to use a `track by` clause, because then `ngOptions` will track the identity
45+
* of the item not by reference, but by the result of the `track by` expression. For example, if your
46+
* collection items have an id property, you would `track by item.id`.
47+
*
48+
* A different issue with objects or collections is that ngModel won't detect if an object property or
49+
* a collection item changes. For that reason, `ngOptions` additionally watches the model using
50+
* `$watchCollection`, when the expression contains a `track by` clause or the the select has the `multiple` attribute.
51+
* This allows ngOptions to trigger a re-rendering of the options even if the actual object/collection
52+
* has not changed identity, but only a property on the object or an item in the collection changes.
4453
*
4554
* Note that `$watchCollection` does a shallow comparison of the properties of the object (or the items in the collection
4655
* if the model is an array). This means that changing a property deeper than the first level inside the
4756
* object/collection will not trigger a re-rendering.
4857
*
49-
*
5058
* ## `select` **`as`**
5159
*
5260
* Using `select` **`as`** will bind the result of the `select` expression to the model, but

0 commit comments

Comments
 (0)