Skip to content

Commit

Permalink
fix(ngShow/ngHide): follow javscript truthy/falsy logic
Browse files Browse the repository at this point in the history
Make ngShow and ngHide follow javascript `truthy`/`falsy` logic and
not the custom toBoolean logic

Fixes angular#5414 angular#4277

BREAKING CHANGE: The expressions

* `<div ng-hide="[]">X</div>`
* `<div ng-hide="'f'">X</div>`
* `<div ng-hide="'[]'">X</div>`

used to be evaluated to `false` and the elements were hidden.

The same effect is present for `ng-show` and the elements are now visible
  • Loading branch information
lgalfaso committed Dec 30, 2013
1 parent 80e7a45 commit fe802c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/ng/directive/ngShowHide.js
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
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
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

0 comments on commit fe802c7

Please sign in to comment.