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

Commit 9767f7b

Browse files
committed
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
1 parent 167aa0c commit 9767f7b

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/ng/directive/select.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,6 @@ var optionDirective = ['$interpolate', function($interpolate) {
521521
return {
522522
restrict: 'E',
523523
priority: 100,
524-
require: '^select',
525524
compile: function(element, attr) {
526525
if (isUndefined(attr.value)) {
527526
var interpolateFn = $interpolate(element.text(), true);
@@ -530,8 +529,13 @@ var optionDirective = ['$interpolate', function($interpolate) {
530529
}
531530
}
532531

533-
return function (scope, element, attr, selectCtrl) {
534-
if (selectCtrl.databound) {
532+
return function (scope, element, attr) {
533+
var selectCtrlName = '$selectController',
534+
parent = element.parent(),
535+
selectCtrl = parent.data(selectCtrlName) ||
536+
parent.parent().data(selectCtrlName); // in case we are in optgroup
537+
538+
if (selectCtrl && selectCtrl.databound) {
535539
// For some reason Opera defaults to true and if not overridden this messes up the repeater.
536540
// We don't want the view to drive the initialization of the model anyway.
537541
element.prop('selected', false);

test/ng/directive/selectSpec.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ describe('select', function() {
11081108
});
11091109

11101110

1111-
describe('OPTION value', function() {
1111+
describe('option', function() {
11121112

11131113
it('should populate value attribute on OPTION', function() {
11141114
compile('<select ng-model="x"><option selected>abc</option></select>');
@@ -1125,5 +1125,18 @@ describe('select', function() {
11251125
compile('<select ng-model="x"><option>hello</select>');
11261126
expect(element).toEqualSelect(['hello']);
11271127
});
1128+
1129+
it('should not blow up when option directive is found inside of a datalist',
1130+
inject(function($compile, $rootScope) {
1131+
var element = $compile('<div>' +
1132+
'<datalist><option>some val</option></datalist>' +
1133+
'<span>{{foo}}</span>' +
1134+
'</div>')($rootScope);
1135+
1136+
$rootScope.foo = 'success';
1137+
$rootScope.$digest();
1138+
expect(element.find('span').text()).toBe('success');
1139+
dealoc(element);
1140+
}));
11281141
});
11291142
});

0 commit comments

Comments
 (0)