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

Commit d85b3f5

Browse files
frederikprijckgkalpak
authored andcommitted
fix(ngValue): correctly update the value property when value is undefined
Previously, when the expression evaluated to `undefined` the `value` property was not updated. This happened because jqLite/jQuery's `prop(_, undefined)` is treated as a getter, thus not apdating the property. This commit fixes it by setting the property to `null` instead. (On IE9 we use `''` - see inline comments for more info.) Fixes #15603 Closes #15605
1 parent 1cf728e commit d85b3f5

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ng/directive/input.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,10 @@ var ngValueDirective = function() {
21422142
* makes it possible to use ngValue as a sort of one-way bind.
21432143
*/
21442144
function updateElementValue(element, attr, value) {
2145-
element.prop('value', value);
2145+
// Support: IE9 only
2146+
// In IE9 values are converted to string (e.g. `input.value = null` results in `input.value === 'null'`).
2147+
var propValue = isDefined(value) ? value : (msie === 9) ? '' : null;
2148+
element.prop('value', propValue);
21462149
attr.$set('value', value);
21472150
}
21482151

test/ng/directive/inputSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -4211,6 +4211,26 @@ describe('input', function() {
42114211
expect(inputElm[0].getAttribute('value')).toBe('something');
42124212
});
42134213

4214+
it('should clear the "dom" value property and attribute when the value is undefined', function() {
4215+
var inputElm = helper.compileInput('<input type="text" ng-value="value">');
4216+
4217+
$rootScope.$apply('value = "something"');
4218+
4219+
expect(inputElm[0].value).toBe('something');
4220+
expect(inputElm[0].getAttribute('value')).toBe('something');
4221+
4222+
$rootScope.$apply(function() {
4223+
delete $rootScope.value;
4224+
});
4225+
4226+
expect(inputElm[0].value).toBe('');
4227+
// Support: IE 9-11
4228+
// In IE it is not possible to remove the `value` attribute from an input element.
4229+
if (!msie) {
4230+
expect(inputElm[0].getAttribute('value')).toBeNull();
4231+
}
4232+
});
4233+
42144234
they('should update the $prop "value" property and attribute after the bound expression changes', {
42154235
input: '<input type="text" ng-value="value">',
42164236
textarea: '<textarea ng-value="value"></textarea>'

0 commit comments

Comments
 (0)