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 +32
-6
lines changed Expand file tree Collapse file tree 2 files changed +32
-6
lines changed Original file line number Diff line number Diff line change @@ -1750,24 +1750,33 @@ var CONSTANT_VALUE_REGEXP = /^(true|false|\d+)$/;
1750
1750
</example>
1751
1751
*/
1752
1752
var ngValueDirective = function ( ) {
1753
+ //helper function
1754
+ var setElementValue = function ( element , attr , value ) {
1755
+ /**
1756
+ * input use 'value attribute' as default value if 'value property has no value'
1757
+ * but once set the 'value property' it will not read value from 'value attribute'
1758
+ * so we set both the value attribute and property here to avoid this problem.
1759
+ */
1760
+ attr . $set ( 'value' , value ) ;
1761
+ element [ 0 ] . value = value ;
1762
+ } ;
1763
+
1753
1764
return {
1754
1765
restrict : 'A' ,
1755
1766
priority : 100 ,
1756
1767
compile : function ( tpl , tplAttr ) {
1757
1768
if ( CONSTANT_VALUE_REGEXP . test ( tplAttr . ngValue ) ) {
1758
1769
return function ngValueConstantLink ( scope , elm , attr ) {
1759
- attr . $set ( 'value' , scope . $eval ( attr . ngValue ) ) ;
1770
+ var value = scope . $eval ( attr . ngValue ) ;
1771
+ setElementValue ( elm , attr , value ) ;
1760
1772
} ;
1761
1773
} else {
1762
1774
return function ngValueLink ( scope , elm , attr ) {
1763
1775
scope . $watch ( attr . ngValue , function valueWatchAction ( value ) {
1764
- attr . $set ( 'value' , value ) ;
1776
+ setElementValue ( elm , attr , value ) ;
1765
1777
} ) ;
1766
1778
} ;
1767
1779
}
1768
1780
}
1769
1781
} ;
1770
- } ;
1771
-
1772
-
1773
-
1782
+ } ;
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