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

Commit

Permalink
fix(select): don't interfere with selection if not databound
Browse files Browse the repository at this point in the history
Closes #926
  • Loading branch information
IgorMinar committed May 2, 2012
1 parent c7f1101 commit 3bd3cc5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
return {
restrict: 'E',
require: ['select', '?ngModel'],
controller: ['$element', '$scope', function($element, $scope) {
controller: ['$element', '$scope', '$attrs', function($element, $scope, $attrs) {
var self = this,
optionsMap = {},
ngModelCtrl = nullModelCtrl,
nullOption,
unknownOption;


self.databound = $attrs.ngModel;


self.init = function(ngModelCtrl_, nullOption_, unknownOption_) {
ngModelCtrl = ngModelCtrl_;
nullOption = nullOption_;
Expand Down Expand Up @@ -509,6 +513,11 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
}];

var optionDirective = ['$interpolate', function($interpolate) {
var nullSelectCtrl = {
addOption: noop,
removeOption: noop
};

return {
restrict: 'E',
priority: 100,
Expand All @@ -521,11 +530,15 @@ var optionDirective = ['$interpolate', function($interpolate) {
}
}

// 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);

return function (scope, element, attr, selectCtrl) {
if (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);
} else {
selectCtrl = nullSelectCtrl;
}

if (interpolateFn) {
scope.$watch(interpolateFn, function(newVal, oldVal) {
attr.$set('value', newVal);
Expand Down
11 changes: 11 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ describe('select', function() {
});


it('should not interfere with selection via selected attr if ngModel directive is not present',
function() {
compile('<select>' +
'<option>not me</option>' +
'<option selected>me!</option>' +
'<option>nah</option>' +
'</select>');
expect(element).toEqualSelect('not me', ['me!'], 'nah');
});


it('should require', function() {
compile(
'<select name="select" ng-model="selection" required ng-change="change()">' +
Expand Down

0 comments on commit 3bd3cc5

Please sign in to comment.