From ac92aea531d27775997454fcc07b7a4dab22f584 Mon Sep 17 00:00:00 2001 From: Jan Varwig Date: Fri, 8 Aug 2014 16:28:57 +0200 Subject: [PATCH] fix(select): Clarify documentation and add exception for ngOptions ngOptions introduced `track by` in c32a859bdb93699cc080f9affed4bcff63005a64. Using `track by` puts constraints on the value you can use in the interpolation expression in ngOptions. This patch both documents this and adds an exception if you use ngOptions in an unsupported way. Closes angular/angular.js#6564 --- src/ng/directive/select.js | 13 +++++++++++++ test/ng/directive/selectSpec.js | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index 8bb93c56ceec..7e58ea68966e 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -70,6 +70,13 @@ var ngOptionsMinErr = minErr('ngOptions'); * * `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`). + *
@@ -337,6 +344,12 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) { // - optionGroupsCache[?][0] is the parent: either the SELECT or OPTGROUP element optionGroupsCache = [[{element: selectElement, label:''}]]; + if (track && match[2] && valueName !== match[1]) { + throw ngOptionsMinErr('trackSelect', + "Do not use 'track by' when your select ('{0}') is different from your value ('{1}')", + match[1], valueName); + } + if (nullOption) { // compile the element since there might be bindings in it $compile(nullOption)(scope); diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index ec40e277cc57..ba243c78d6fc 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -850,6 +850,15 @@ describe('select', function() { expect(element.val()).toEqual('4'); }); + it('should throw an error when trying to combine track by with a complex select expression', function() { + expect(function() { + createSelect({ + 'ng-model': 'selected', + 'ng-options': 'item.id as item.name for item in values track by item.id' + }); + }).toThrowMinErr('ngOptions','trackSelect', "Do not use 'track by' when your select ('item.id') is different from your value ('item')"); + }); + it('should bind to scope value through experession', function() { createSelect({