diff --git a/docs/content/error/ngOptions/trkslct.ngdoc b/docs/content/error/ngOptions/trkslct.ngdoc new file mode 100644 index 000000000000..836fce31df4a --- /dev/null +++ b/docs/content/error/ngOptions/trkslct.ngdoc @@ -0,0 +1,28 @@ +@ngdoc error +@name ngOptions:trkslct +@fullName Comprehension expression cannot contain both selectAs and trackBy 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: ` +``` + +For more information on valid expression syntax, see 'ngOptions' in {@link ng.directive:select select} directive docs. diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index 46d38717c37b..c840716cc6f2 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -35,6 +35,12 @@ var ngOptionsMinErr = minErr('ngOptions'); * be bound to string values at present. * * + *
+ * **Note:** Using `selectAs` will bind the result of the `selectAs` expression to the model, but + * the value of the `select` and `option` elements will be either the index (for array data sources) + * or property name (for object data sources) of the value within the collection. + *
+ * * @param {string} ngModel Assignable angular expression to data-bind to. * @param {string=} name Property name of the form under which the control is published. * @param {string=} required The control is considered valid only if value is entered. @@ -69,7 +75,25 @@ var ngOptionsMinErr = minErr('ngOptions'); * DOM element. * * `trackexpr`: Used when working with an array of objects. The result of this expression will be * used to identify the objects in the array. The `trackexpr` will most likely refer to the - * `value` variable (e.g. `value.propertyName`). + * `value` variable (e.g. `value.propertyName`). With this the selection is preserved + * even when the options are recreated (e.g. reloaded from the server). + + *
+ * **Note:** Using `selectAs` together with `trackexpr` is not possible (and will throw). + * Reasoning: + * - Example: ' + ); + + scope.$apply(function() { + scope.values = {a: {id: 10, name: 'A'}, b: {id: 20, name: 'B'}}; + scope.selected = scope.values.a.id; + }); + + scope.$apply(function() { + scope.values.a.name = 'C'; + }); + + var options = element.find('option'); + expect(options.length).toEqual(2); + expect(sortedHtml(options[0])).toEqual(''); + expect(sortedHtml(options[1])).toEqual(''); + }); + + it('should bind to object key', function() { createSelect({ 'ng-model': 'selected',