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

Commit c179cce

Browse files
committed
fix(ngValue): set value property instead of attr
in case of input element set value using property instead of attribute fixed #13984
1 parent b8b5b88 commit c179cce

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/ng/directive/input.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1756,12 +1756,25 @@ var ngValueDirective = function() {
17561756
compile: function(tpl, tplAttr) {
17571757
if (CONSTANT_VALUE_REGEXP.test(tplAttr.ngValue)) {
17581758
return function ngValueConstantLink(scope, elm, attr) {
1759-
attr.$set('value', scope.$eval(attr.ngValue));
1759+
var value = scope.$eval(attr.ngValue);
1760+
/**
1761+
* input use 'value attribute' as default value if 'value property has no value'
1762+
* but once set the 'value property' it will not read value from 'value attribute'
1763+
* so we set both the value attribute and property here to avoid this problem.
1764+
*/
1765+
attr.$set('value', value);
1766+
elm[0].value = value;
17601767
};
17611768
} else {
17621769
return function ngValueLink(scope, elm, attr) {
17631770
scope.$watch(attr.ngValue, function valueWatchAction(value) {
1771+
/**
1772+
* input use 'value attribute' as default value if 'value property has no value'
1773+
* but once set the 'value property' it will not read value from 'value attribute'
1774+
* so we set both the value attribute and property here to avoid this problem.
1775+
*/
17641776
attr.$set('value', value);
1777+
elm[0].value = value;
17651778
});
17661779
};
17671780
}

test/ng/directive/inputSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,6 +3046,23 @@ describe('input', function() {
30463046
expect(inputElm[0].getAttribute('value')).toBe('something');
30473047
});
30483048

3049+
it('should update the input "value" property and attribute after change the "value" property', function() {
3050+
var inputElm = helper.compileInput('<input type="text" ng-value="value">');
3051+
3052+
$rootScope.$apply(function() {
3053+
$rootScope.value = 'something';
3054+
});
3055+
expect(inputElm[0].value).toBe('something');
3056+
expect(inputElm[0].getAttribute('value')).toBe('something');
3057+
3058+
helper.changeInputValueTo('newValue');
3059+
3060+
$rootScope.$apply(function() {
3061+
$rootScope.value = 'anotherValue';
3062+
});
3063+
expect(inputElm[0].value).toBe('anotherValue');
3064+
expect(inputElm[0].getAttribute('value')).toBe('anotherValue');
3065+
});
30493066

30503067
it('should evaluate and set constant expressions', function() {
30513068
var inputElm = helper.compileInput('<input type="radio" ng-model="selected" ng-value="true">' +

0 commit comments

Comments
 (0)