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

fix(select): don't register options when select has no ngModel #14864

Merged
merged 1 commit into from
Jul 6, 2016
Merged
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
11 changes: 8 additions & 3 deletions src/ng/directive/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,16 @@ var selectDirective = function() {

function selectPreLink(scope, element, attr, ctrls) {

// if ngModel is not defined, we don't need to do anything
var selectCtrl = ctrls[0];
var ngModelCtrl = ctrls[1];
if (!ngModelCtrl) return;

var selectCtrl = ctrls[0];
// if ngModel is not defined, we don't need to do anything but set the registerOption
// function to noop, so options don't get added internally
if (!ngModelCtrl) {
selectCtrl.registerOption = noop;
return;
}


selectCtrl.ngModelCtrl = ngModelCtrl;

Expand Down
24 changes: 24 additions & 0 deletions test/ng/directive/selectSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,30 @@ describe('select', function() {

});

it('should not add options to the select if ngModel is not present', inject(function($rootScope) {
var scope = $rootScope;
scope.d = 'd';
scope.e = 'e';
scope.f = 'f';

compile('<select>' +
'<option ng-value="\'a\'">alabel</option>' +
'<option value="b">blabel</option>' +
'<option >c</option>' +
'<option ng-value="d">dlabel</option>' +
'<option value="{{e}}">elabel</option>' +
'<option>{{f}}</option>' +
'</select>');

var selectCtrl = element.controller('select');

expect(selectCtrl.hasOption('a')).toBe(false);
expect(selectCtrl.hasOption('b')).toBe(false);
expect(selectCtrl.hasOption('c')).toBe(false);
expect(selectCtrl.hasOption('d')).toBe(false);
expect(selectCtrl.hasOption('e')).toBe(false);
expect(selectCtrl.hasOption('f')).toBe(false);
}));

describe('select-one', function() {

Expand Down