From 800d67ea101b65cb921f2eba7f00f2619bbf0c1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Galfas=C3=B3?= Date: Mon, 30 Dec 2013 12:52:37 -0300 Subject: [PATCH] fix(ngShow/ngHide): follow javscript `truthy`/`falsy` logic Make ngShow and ngHide follow javascript `truthy`/`falsy` logic and not the custom toBoolean logic Fixes #5414 #4277 --- src/ng/directive/ngShowHide.js | 8 ++++---- test/BinderSpec.js | 4 ++-- test/ng/directive/ngShowHideSpec.js | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/ng/directive/ngShowHide.js b/src/ng/directive/ngShowHide.js index a38a7e7af253..9a8f6cebefa2 100644 --- a/src/ng/directive/ngShowHide.js +++ b/src/ng/directive/ngShowHide.js @@ -142,8 +142,8 @@ */ var ngShowDirective = ['$animate', function($animate) { return function(scope, element, attr) { - scope.$watch(attr.ngShow, function ngShowWatchAction(value){ - $animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'ng-hide'); + scope.$watch('!!(' + attr.ngShow + ')', function ngShowWatchAction(value){ + $animate[value ? 'removeClass' : 'addClass'](element, 'ng-hide'); }); }; }]; @@ -291,8 +291,8 @@ var ngShowDirective = ['$animate', function($animate) { */ var ngHideDirective = ['$animate', function($animate) { return function(scope, element, attr) { - scope.$watch(attr.ngHide, function ngHideWatchAction(value){ - $animate[toBoolean(value) ? 'addClass' : 'removeClass'](element, 'ng-hide'); + scope.$watch('!!(' + attr.ngHide + ')', function ngHideWatchAction(value){ + $animate[value ? 'addClass' : 'removeClass'](element, 'ng-hide'); }); }; }]; diff --git a/test/BinderSpec.js b/test/BinderSpec.js index b553c68dcfd0..8f1a9da1abc4 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -272,7 +272,7 @@ describe('Binder', function() { $rootScope.hidden = 'false'; $rootScope.$apply(); - assertVisible(element); + assertHidden(element); $rootScope.hidden = ''; $rootScope.$apply(); @@ -291,7 +291,7 @@ describe('Binder', function() { $rootScope.show = 'false'; $rootScope.$apply(); - assertHidden(element); + assertVisible(element); $rootScope.show = ''; $rootScope.$apply(); diff --git a/test/ng/directive/ngShowHideSpec.js b/test/ng/directive/ngShowHideSpec.js index 4a8e55da386e..1b55c9d11c3b 100644 --- a/test/ng/directive/ngShowHideSpec.js +++ b/test/ng/directive/ngShowHideSpec.js @@ -28,6 +28,17 @@ describe('ngShow / ngHide', function() { $rootScope.$digest(); expect(element).toBeShown(); })); + + it('should follow javascript `truthy`/`falsy` logic', inject(function($rootScope, $compile) { + var cases = ['[]', 'f', [], [''], 'false', {}, function() {}, function(f) {}, 0, false, null, undefined, '', NaN]; + element = jqLite('
'); + element = $compile(element)($rootScope); + angular.forEach(cases, function(value) { + $rootScope.exp = value; + $rootScope.$digest(); + expect(element)[value ? 'toBeShown' : 'toBeHidden'](); + }); + })); }); describe('ngHide', function() { @@ -39,6 +50,17 @@ describe('ngShow / ngHide', function() { $rootScope.$digest(); expect(element).toBeHidden(); })); + + it('should follow javascript `truthy`/`falsy` logic', inject(function($rootScope, $compile) { + var cases = ['[]', 'f', [], [''], 'false', {}, function() {}, function(f) {}, 0, false, null, undefined, '', NaN]; + element = jqLite('
'); + element = $compile(element)($rootScope); + angular.forEach(cases, function(value) { + $rootScope.exp = value; + $rootScope.$digest(); + expect(element)[value ? 'toBeHidden' : 'toBeShown'](); + }); + })); }); });