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

fix(ngShow/ngHide): follow javscript truthy/falsy logic #5573

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ng/directive/ngShowHide.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
};
}];
Expand Down Expand Up @@ -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');
});
};
}];
4 changes: 2 additions & 2 deletions test/BinderSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ describe('Binder', function() {
$rootScope.hidden = 'false';
$rootScope.$apply();

assertVisible(element);
assertHidden(element);

$rootScope.hidden = '';
$rootScope.$apply();
Expand All @@ -291,7 +291,7 @@ describe('Binder', function() {
$rootScope.show = 'false';
$rootScope.$apply();

assertHidden(element);
assertVisible(element);

$rootScope.show = '';
$rootScope.$apply();
Expand Down
22 changes: 22 additions & 0 deletions test/ng/directive/ngShowHideSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div ng-show="exp"></div>');
element = $compile(element)($rootScope);
angular.forEach(cases, function(value) {
$rootScope.exp = value;
$rootScope.$digest();
expect(element)[value ? 'toBeShown' : 'toBeHidden']();
});
}));
});

describe('ngHide', function() {
Expand All @@ -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('<div ng-hide="exp"></div>');
element = $compile(element)($rootScope);
angular.forEach(cases, function(value) {
$rootScope.exp = value;
$rootScope.$digest();
expect(element)[value ? 'toBeHidden' : 'toBeShown']();
});
}));
});
});

Expand Down