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

Commit f717416

Browse files
shahatajeffbcross
authored andcommitted
fix(select): use $viewValue instead of $modelValue
Closes #8929
1 parent 0656484 commit f717416

File tree

2 files changed

+134
-10
lines changed

2 files changed

+134
-10
lines changed

src/ng/directive/select.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -436,16 +436,16 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
436436
function getSelectedSet() {
437437
var selectedSet = false;
438438
if (multiple) {
439-
var modelValue = ctrl.$modelValue;
440-
if (trackFn && isArray(modelValue)) {
439+
var viewValue = ctrl.$viewValue;
440+
if (trackFn && isArray(viewValue)) {
441441
selectedSet = new HashMap([]);
442442
var locals = {};
443-
for (var trackIndex = 0; trackIndex < modelValue.length; trackIndex++) {
444-
locals[valueName] = modelValue[trackIndex];
445-
selectedSet.put(trackFn(scope, locals), modelValue[trackIndex]);
443+
for (var trackIndex = 0; trackIndex < viewValue.length; trackIndex++) {
444+
locals[valueName] = viewValue[trackIndex];
445+
selectedSet.put(trackFn(scope, locals), viewValue[trackIndex]);
446446
}
447447
} else {
448-
selectedSet = new HashMap(modelValue);
448+
selectedSet = new HashMap(viewValue);
449449
}
450450
}
451451
return selectedSet;
@@ -470,7 +470,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
470470
optionGroup,
471471
option,
472472
existingParent, existingOptions, existingOption,
473-
modelValue = ctrl.$modelValue,
473+
viewValue = ctrl.$viewValue,
474474
values = valuesFn(scope) || [],
475475
keys = keyName ? sortedKeys(values) : values,
476476
key,
@@ -508,10 +508,10 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
508508
} else {
509509
if (trackFn) {
510510
var modelCast = {};
511-
modelCast[valueName] = modelValue;
511+
modelCast[valueName] = viewValue;
512512
selected = trackFn(scope, modelCast) === trackFn(scope, locals);
513513
} else {
514-
selected = modelValue === valueFn(scope, locals);
514+
selected = viewValue === valueFn(scope, locals);
515515
}
516516
selectedSet = selectedSet || selected; // see if at least one item is selected
517517
}
@@ -527,7 +527,7 @@ var selectDirective = ['$compile', '$parse', function($compile, $parse) {
527527
});
528528
}
529529
if (!multiple) {
530-
if (nullOption || modelValue === null) {
530+
if (nullOption || viewValue === null) {
531531
// insert null option if we have a placeholder, or the model is null
532532
optionGroups[''].unshift({id:'', label:'', selected:!selectedSet});
533533
} else if (!selectedSet) {

test/ng/directive/selectSpec.js

+124
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,130 @@ describe('select', function() {
15501550
expect(scope.value).toBe(false);
15511551
});
15521552
});
1553+
1554+
describe('ngModelCtrl', function() {
1555+
it('should prefix the model value with the word "the" using $parsers', function() {
1556+
createSelect({
1557+
'name': 'select',
1558+
'ng-model': 'value',
1559+
'ng-options': 'item for item in [\'first\', \'second\', \'third\', \'fourth\']',
1560+
});
1561+
1562+
scope.form.select.$parsers.push(function(value) {
1563+
return 'the ' + value;
1564+
});
1565+
1566+
element.val('2');
1567+
browserTrigger(element, 'change');
1568+
expect(scope.value).toBe('the third');
1569+
expect(element.val()).toBe('2');
1570+
});
1571+
1572+
it('should prefix the view value with the word "the" using $formatters', function() {
1573+
createSelect({
1574+
'name': 'select',
1575+
'ng-model': 'value',
1576+
'ng-options': 'item for item in [\'the first\', \'the second\', \'the third\', \'the fourth\']',
1577+
});
1578+
1579+
scope.form.select.$formatters.push(function(value) {
1580+
return 'the ' + value;
1581+
});
1582+
1583+
scope.$apply(function() {
1584+
scope.value = 'third';
1585+
});
1586+
expect(element.val()).toBe('2');
1587+
});
1588+
1589+
it('should fail validation when $validators fail', function() {
1590+
createSelect({
1591+
'name': 'select',
1592+
'ng-model': 'value',
1593+
'ng-options': 'item for item in [\'first\', \'second\', \'third\', \'fourth\']',
1594+
});
1595+
1596+
scope.form.select.$validators.fail = function() {
1597+
return false;
1598+
};
1599+
1600+
element.val('2');
1601+
browserTrigger(element, 'change');
1602+
expect(element).toBeInvalid();
1603+
expect(scope.value).toBeUndefined();
1604+
expect(element.val()).toBe('2');
1605+
});
1606+
1607+
it('should pass validation when $validators pass', function() {
1608+
createSelect({
1609+
'name': 'select',
1610+
'ng-model': 'value',
1611+
'ng-options': 'item for item in [\'first\', \'second\', \'third\', \'fourth\']',
1612+
});
1613+
1614+
scope.form.select.$validators.pass = function() {
1615+
return true;
1616+
};
1617+
1618+
element.val('2');
1619+
browserTrigger(element, 'change');
1620+
expect(element).toBeValid();
1621+
expect(scope.value).toBe('third');
1622+
expect(element.val()).toBe('2');
1623+
});
1624+
1625+
it('should fail validation when $asyncValidators fail', inject(function($q, $rootScope) {
1626+
var defer;
1627+
createSelect({
1628+
'name': 'select',
1629+
'ng-model': 'value',
1630+
'ng-options': 'item for item in [\'first\', \'second\', \'third\', \'fourth\']',
1631+
});
1632+
1633+
scope.form.select.$asyncValidators.async = function() {
1634+
defer = $q.defer();
1635+
return defer.promise;
1636+
};
1637+
1638+
element.val('2');
1639+
browserTrigger(element, 'change');
1640+
expect(scope.form.select.$pending).toBeDefined();
1641+
expect(scope.value).toBeUndefined();
1642+
expect(element.val()).toBe('2');
1643+
1644+
defer.reject();
1645+
$rootScope.$digest();
1646+
expect(scope.form.select.$pending).toBeUndefined();
1647+
expect(scope.value).toBeUndefined();
1648+
expect(element.val()).toBe('2');
1649+
}));
1650+
1651+
it('should pass validation when $asyncValidators pass', inject(function($q, $rootScope) {
1652+
var defer;
1653+
createSelect({
1654+
'name': 'select',
1655+
'ng-model': 'value',
1656+
'ng-options': 'item for item in [\'first\', \'second\', \'third\', \'fourth\']',
1657+
});
1658+
1659+
scope.form.select.$asyncValidators.async = function() {
1660+
defer = $q.defer();
1661+
return defer.promise;
1662+
};
1663+
1664+
element.val('2');
1665+
browserTrigger(element, 'change');
1666+
expect(scope.form.select.$pending).toBeDefined();
1667+
expect(scope.value).toBeUndefined();
1668+
expect(element.val()).toBe('2');
1669+
1670+
defer.resolve();
1671+
$rootScope.$digest();
1672+
expect(scope.form.select.$pending).toBeUndefined();
1673+
expect(scope.value).toBe('third');
1674+
expect(element.val()).toBe('2');
1675+
}));
1676+
});
15531677
});
15541678

15551679

0 commit comments

Comments
 (0)