This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -1756,12 +1756,25 @@ var ngValueDirective = function() {
1756
1756
compile : function ( tpl , tplAttr ) {
1757
1757
if ( CONSTANT_VALUE_REGEXP . test ( tplAttr . ngValue ) ) {
1758
1758
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 ;
1760
1767
} ;
1761
1768
} else {
1762
1769
return function ngValueLink ( scope , elm , attr ) {
1763
1770
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
+ */
1764
1776
attr . $set ( 'value' , value ) ;
1777
+ elm [ 0 ] . value = value ;
1765
1778
} ) ;
1766
1779
} ;
1767
1780
}
Original file line number Diff line number Diff line change @@ -3046,6 +3046,23 @@ describe('input', function() {
3046
3046
expect ( inputElm [ 0 ] . getAttribute ( 'value' ) ) . toBe ( 'something' ) ;
3047
3047
} ) ;
3048
3048
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
+ } ) ;
3049
3066
3050
3067
it ( 'should evaluate and set constant expressions' , function ( ) {
3051
3068
var inputElm = helper . compileInput ( '<input type="radio" ng-model="selected" ng-value="true">' +
You can’t perform that action at this time.
0 commit comments