Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit dbd3db8

Browse files
fix(ngOptions): disable select directive when ngOptions is active
1 parent 1c2d2e8 commit dbd3db8

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/ng/directive/ngOptions.js

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
395395
return {
396396
restrict: 'A',
397397
terminal: true,
398+
controller: function() { },
398399
require: ['select', 'ngModel'],
399400
link: function(scope, selectElement, attr, ctrls) {
400401

src/ng/directive/select.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,15 @@ var optionDirective = ['$interpolate', function($interpolate) {
417417

418418
// This is an optimization over using ^^ since we don't want to have to search
419419
// all the way to the root of the DOM for every single option element
420-
var selectCtrlName = '$selectController',
421-
parent = element.parent(),
422-
selectCtrl = parent.data(selectCtrlName) ||
423-
parent.parent().data(selectCtrlName); // in case we are in optgroup
420+
var selectElement = element.parent();
421+
if (nodeName_(selectElement) !== 'select') {
422+
selectElement = element.parent(); // in case we are in optgroup
423+
}
424+
425+
var selectCtrl = selectElement.data('$selectController');
426+
427+
// If there is an ngOptions directive then bail out
428+
if (selectElement.data('$ngOptionsController')) return;
424429

425430
function addOption(optionValue) {
426431
selectCtrl.addOption(optionValue, element);

test/ng/directive/ngOptionsSpec.js

+23
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ describe('ngOptions', function() {
104104
});
105105
});
106106

107+
beforeEach(module(function($compileProvider) {
108+
$compileProvider
109+
.directive('compileContents', function($compile) {
110+
return {
111+
link: { pre: function(scope, element) {
112+
$compile(element.contents())(scope);
113+
}}
114+
};
115+
});
116+
}));
117+
107118
beforeEach(inject(function($rootScope, _$compile_) {
108119
scope = $rootScope.$new(); //create a child scope because the root scope can't be $destroy-ed
109120
$compile = _$compile_;
@@ -2119,6 +2130,18 @@ describe('ngOptions', function() {
21192130
option = element.find('option').eq(0);
21202131
expect(option.text()).toBe('A');
21212132
});
2133+
2134+
2135+
it('should not throw when a directive compiles the blank option before ngOptions is linked', function() {
2136+
expect(function() {
2137+
createSelect({
2138+
'compile-contents': '',
2139+
'name': 'select',
2140+
'ng-model': 'value',
2141+
'ng-options': 'item for item in items',
2142+
}, true);
2143+
}).not.toThrow();
2144+
});
21222145
});
21232146

21242147

0 commit comments

Comments
 (0)