From ccaed256df641a0cbe80d6c0d51dcf06930ab7c0 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 10 Aug 2012 15:38:05 -0700 Subject: [PATCH] fix(option): support option elements in datalist previously we expected to find option elements only within select element and if that was not the case we throw an error. This made it impossible to include datalist element with nested option elements in the template. Closes #1165 --- src/ng/directive/select.js | 10 +++++++--- test/ng/directive/selectSpec.js | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ng/directive/select.js b/src/ng/directive/select.js index 27a92c4877d9..77b2f46d23d3 100644 --- a/src/ng/directive/select.js +++ b/src/ng/directive/select.js @@ -521,7 +521,6 @@ var optionDirective = ['$interpolate', function($interpolate) { return { restrict: 'E', priority: 100, - require: '^select', compile: function(element, attr) { if (isUndefined(attr.value)) { var interpolateFn = $interpolate(element.text(), true); @@ -530,8 +529,13 @@ var optionDirective = ['$interpolate', function($interpolate) { } } - return function (scope, element, attr, selectCtrl) { - if (selectCtrl.databound) { + return function (scope, element, attr) { + var selectCtrlName = '$selectController', + parent = element.parent(), + selectCtrl = parent.data(selectCtrlName) || + parent.parent().data(selectCtrlName); // in case we are in optgroup + + if (selectCtrl && selectCtrl.databound) { // For some reason Opera defaults to true and if not overridden this messes up the repeater. // We don't want the view to drive the initialization of the model anyway. element.prop('selected', false); diff --git a/test/ng/directive/selectSpec.js b/test/ng/directive/selectSpec.js index 321990e80d18..c63d17bfaa66 100644 --- a/test/ng/directive/selectSpec.js +++ b/test/ng/directive/selectSpec.js @@ -1108,7 +1108,7 @@ describe('select', function() { }); - describe('OPTION value', function() { + describe('option', function() { it('should populate value attribute on OPTION', function() { compile(''); @@ -1125,5 +1125,18 @@ describe('select', function() { compile(''); expect(element).toEqualSelect(['hello']); }); + + it('should not blow up when option directive is found inside of a datalist', + inject(function($compile, $rootScope) { + var element = $compile('
' + + '' + + '{{foo}}' + + '
')($rootScope); + + $rootScope.foo = 'success'; + $rootScope.$digest(); + expect(element.find('span').text()).toBe('success'); + dealoc(element); + })); }); });