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

Commit 3bd3cc5

Browse files
committed
fix(select): don't interfere with selection if not databound
Closes #926
1 parent c7f1101 commit 3bd3cc5

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/ng/directive/select.js

+18-5
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,17 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
126126
return {
127127
restrict: 'E',
128128
require: ['select', '?ngModel'],
129-
controller: ['$element', '$scope', function($element, $scope) {
129+
controller: ['$element', '$scope', '$attrs', function($element, $scope, $attrs) {
130130
var self = this,
131131
optionsMap = {},
132132
ngModelCtrl = nullModelCtrl,
133133
nullOption,
134134
unknownOption;
135135

136+
137+
self.databound = $attrs.ngModel;
138+
139+
136140
self.init = function(ngModelCtrl_, nullOption_, unknownOption_) {
137141
ngModelCtrl = ngModelCtrl_;
138142
nullOption = nullOption_;
@@ -509,6 +513,11 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
509513
}];
510514

511515
var optionDirective = ['$interpolate', function($interpolate) {
516+
var nullSelectCtrl = {
517+
addOption: noop,
518+
removeOption: noop
519+
};
520+
512521
return {
513522
restrict: 'E',
514523
priority: 100,
@@ -521,11 +530,15 @@ var optionDirective = ['$interpolate', function($interpolate) {
521530
}
522531
}
523532

524-
// For some reason Opera defaults to true and if not overridden this messes up the repeater.
525-
// We don't want the view to drive the initialization of the model anyway.
526-
element.prop('selected', false);
527-
528533
return function (scope, element, attr, selectCtrl) {
534+
if (selectCtrl.databound) {
535+
// For some reason Opera defaults to true and if not overridden this messes up the repeater.
536+
// We don't want the view to drive the initialization of the model anyway.
537+
element.prop('selected', false);
538+
} else {
539+
selectCtrl = nullSelectCtrl;
540+
}
541+
529542
if (interpolateFn) {
530543
scope.$watch(interpolateFn, function(newVal, oldVal) {
531544
attr.$set('value', newVal);

test/ng/directive/selectSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ describe('select', function() {
6161
});
6262

6363

64+
it('should not interfere with selection via selected attr if ngModel directive is not present',
65+
function() {
66+
compile('<select>' +
67+
'<option>not me</option>' +
68+
'<option selected>me!</option>' +
69+
'<option>nah</option>' +
70+
'</select>');
71+
expect(element).toEqualSelect('not me', ['me!'], 'nah');
72+
});
73+
74+
6475
it('should require', function() {
6576
compile(
6677
'<select name="select" ng-model="selection" required ng-change="change()">' +

0 commit comments

Comments
 (0)