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

fix(ngOptions): disable select directive when ngOptions is active #12972

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ng/directive/ngOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ var ngOptionsDirective = ['$compile', '$parse', function($compile, $parse) {
return {
restrict: 'A',
terminal: true,
controller: function() { },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noop?

require: ['select', 'ngModel'],
link: function(scope, selectElement, attr, ctrls) {

Expand Down
13 changes: 9 additions & 4 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,10 +417,15 @@ var optionDirective = ['$interpolate', function($interpolate) {

// This is an optimization over using ^^ since we don't want to have to search
// all the way to the root of the DOM for every single option element
var selectCtrlName = '$selectController',
parent = element.parent(),
selectCtrl = parent.data(selectCtrlName) ||
parent.parent().data(selectCtrlName); // in case we are in optgroup
var selectElement = element.parent();
if (nodeName_(selectElement) !== 'select') {
selectElement = selectElement.parent(); // in case we are in optgroup
}

var selectCtrl = selectElement.data('$selectController');

// If there is an ngOptions directive then bail out
if (selectElement.data('$ngOptionsController')) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally prefer selectElement.controller('select') Is the other one faster?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, .controller(...) will keep searching up the tree till it hits the root


function addOption(optionValue) {
selectCtrl.addOption(optionValue, element);
Expand Down
49 changes: 49 additions & 0 deletions test/ng/directive/ngOptionsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,30 @@ describe('ngOptions', function() {
});
});

beforeEach(module(function($compileProvider) {
$compileProvider
.directive('compileContents', function($compile) {
return {
link: {
pre: function(scope, element) {
$compile(element.contents())(scope);
}
}
};
})
.directive('customSelect', function() {
return {
restrict: 'E',
replace: true,
scope: { ngModel: '=', options: '='},
templateUrl: 'select_template.html',
link: function(scope) {
scope.selectable_options = scope.options;
}
}
});
}));

beforeEach(inject(function($rootScope, _$compile_) {
scope = $rootScope.$new(); //create a child scope because the root scope can't be $destroy-ed
$compile = _$compile_;
Expand Down Expand Up @@ -2119,6 +2143,31 @@ describe('ngOptions', function() {
option = element.find('option').eq(0);
expect(option.text()).toBe('A');
});


it('should not throw when a directive compiles the blank option before ngOptions is linked', function() {
expect(function() {
createSelect({
'compile-contents': '',
'name': 'select',
'ng-model': 'value',
'ng-options': 'item for item in items',
}, true);
}).not.toThrow();
});

it('should not throw with a directive that replaces', inject(function($templateCache, $httpBackend) {
$templateCache.put('select_template.html', '<select ng-options="option as option for option in selectable_options"> <option value="">This is a test</option> </select>');

scope.options = ['a', 'b', 'c', 'd'];

expect(function() {
var element = $compile('<custom-select ng-model="value" options="options"></custom-select>')(scope);
scope.$digest();
dealoc(element);
}).not.toThrow();

}));
});


Expand Down