This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File tree 2 files changed +34
-6
lines changed
2 files changed +34
-6
lines changed Original file line number Diff line number Diff line change 341
341
342
342
var ngAttributeAliasDirectives = { } ;
343
343
344
-
345
344
// boolean attrs are evaluated
346
345
forEach ( BOOLEAN_ATTR , function ( propName , attrName ) {
347
346
// binding to multiple is not supported
348
347
if ( propName == "multiple" ) return ;
349
348
349
+ function defaultLinkFn ( scope , element , attr ) {
350
+ scope . $watch ( attr [ normalized ] , function ngBooleanAttrWatchAction ( value ) {
351
+ attr . $set ( attrName , ! ! value ) ;
352
+ } ) ;
353
+ }
354
+
350
355
var normalized = directiveNormalize ( 'ng-' + attrName ) ;
356
+ var linkFn = defaultLinkFn ;
357
+
358
+ if ( propName === 'checked' ) {
359
+ linkFn = function ( scope , element , attr ) {
360
+ // ensuring ngChecked doesn't interfere with ngModel when both are set on the same input
361
+ if ( attr . ngModel !== attr [ normalized ] ) {
362
+ defaultLinkFn ( scope , element , attr ) ;
363
+ }
364
+ } ;
365
+ }
366
+
351
367
ngAttributeAliasDirectives [ normalized ] = function ( ) {
352
368
return {
353
369
restrict : 'A' ,
354
370
priority : 100 ,
355
- link : function ( scope , element , attr ) {
356
- scope . $watch ( attr [ normalized ] , function ngBooleanAttrWatchAction ( value ) {
357
- attr . $set ( attrName , ! ! value ) ;
358
- } ) ;
359
- }
371
+ link : linkFn
360
372
} ;
361
373
} ;
362
374
} ) ;
Original file line number Diff line number Diff line change @@ -42,6 +42,22 @@ describe('boolean attr directives', function() {
42
42
} ) ) ;
43
43
44
44
45
+ it ( 'should not bind checked when ngModel is present' , inject ( function ( $rootScope , $compile ) {
46
+ // test for https://github.com/angular/angular.js/issues/10662
47
+ element = $compile ( '<input type="checkbox" ng-model="value" ng-false-value="\'false\'" ' +
48
+ 'ng-true-value="\'true\'" ng-checked="value" />' ) ( $rootScope ) ;
49
+ $rootScope . value = 'true' ;
50
+ $rootScope . $digest ( ) ;
51
+ expect ( element [ 0 ] . checked ) . toBe ( true ) ;
52
+ browserTrigger ( element , 'click' ) ;
53
+ expect ( element [ 0 ] . checked ) . toBe ( false ) ;
54
+ expect ( $rootScope . value ) . toBe ( 'false' ) ;
55
+ browserTrigger ( element , 'click' ) ;
56
+ expect ( element [ 0 ] . checked ) . toBe ( true ) ;
57
+ expect ( $rootScope . value ) . toBe ( 'true' ) ;
58
+ } ) ) ;
59
+
60
+
45
61
it ( 'should bind selected' , inject ( function ( $rootScope , $compile ) {
46
62
element = $compile ( '<select><option value=""></option><option ng-selected="isSelected">Greetings!</option></select>' ) ( $rootScope ) ;
47
63
jqLite ( document . body ) . append ( element ) ;
You can’t perform that action at this time.
0 commit comments