Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4e4c9e9

Browse files
committedFeb 5, 2014
fix(ingIf/ngShow/ngHide): follow javscript truthy/falsy logic
Make ngIf, ngShow and ngHide follow javascript `truthy`/`falsy` logic and not the custom toBoolean logic Fixes angular#5414 angular#4277 angular#3969 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; and with `ng-if` and the elements are now removed If you were previously doing `ng-show="exp"` where `$scope.exp = 'no' // (or 'n' or 'f')`, then instead write `ng-show="exp && exp !== 'no'` (or 'n' or 'f').
1 parent 95522cc commit 4e4c9e9

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed
 

‎src/ng/directive/ngIf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ var ngIfDirective = ['$animate', function($animate) {
8585
$$tlb: true,
8686
link: function ($scope, $element, $attr, ctrl, $transclude) {
8787
var block, childScope;
88-
$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {
88+
$scope.$watch('!!(' + $attr.ngIf + ')', function ngIfWatchAction(value) {
8989

90-
if (toBoolean(value)) {
90+
if (value) {
9191
if (!childScope) {
9292
childScope = $scope.$new();
9393
$transclude(childScope, function (clone) {

‎src/ng/directive/ngShowHide.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@
150150
*/
151151
var ngShowDirective = ['$animate', function($animate) {
152152
return function(scope, element, attr) {
153-
scope.$watch(attr.ngShow, function ngShowWatchAction(value){
154-
$animate[toBoolean(value) ? 'removeClass' : 'addClass'](element, 'ng-hide');
153+
scope.$watch('!!(' + attr.ngShow + ')', function ngShowWatchAction(value){
154+
$animate[value ? 'removeClass' : 'addClass'](element, 'ng-hide');
155155
});
156156
};
157157
}];
@@ -307,8 +307,8 @@ var ngShowDirective = ['$animate', function($animate) {
307307
*/
308308
var ngHideDirective = ['$animate', function($animate) {
309309
return function(scope, element, attr) {
310-
scope.$watch(attr.ngHide, function ngHideWatchAction(value){
311-
$animate[toBoolean(value) ? 'addClass' : 'removeClass'](element, 'ng-hide');
310+
scope.$watch('!!(' + attr.ngHide + ')', function ngHideWatchAction(value){
311+
$animate[value ? 'addClass' : 'removeClass'](element, 'ng-hide');
312312
});
313313
};
314314
}];

‎test/BinderSpec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ describe('Binder', function() {
272272
$rootScope.hidden = 'false';
273273
$rootScope.$apply();
274274

275-
assertVisible(element);
275+
assertHidden(element);
276276

277277
$rootScope.hidden = '';
278278
$rootScope.$apply();
@@ -291,7 +291,7 @@ describe('Binder', function() {
291291
$rootScope.show = 'false';
292292
$rootScope.$apply();
293293

294-
assertHidden(element);
294+
assertVisible(element);
295295

296296
$rootScope.show = '';
297297
$rootScope.$apply();

‎test/ng/directive/ngIfSpec.js

+12
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ describe('ngIf', function () {
136136
expect(element.text()).toBe('before;after;');
137137
});
138138

139+
it('should evaluate using javascript `truthy`/`falsy` logic', function () {
140+
var cases = ['[]', 'f', [], [''], 'false', {}, function() {}, function(f) {}, 0, false, null, undefined, '', NaN];
141+
element.append($compile(
142+
'<div ng-if="value">Lucas</div>'
143+
)($scope));
144+
angular.forEach(cases, function(value) {
145+
$scope.value = value;
146+
$scope.$apply();
147+
expect(element.text()).toBe(value ? 'Lucas' : '');
148+
});
149+
});
150+
139151
it('should restore the element to its compiled state', function() {
140152
$scope.value = true;
141153
makeIf('value');

‎test/ng/directive/ngShowHideSpec.js

+24
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ describe('ngShow / ngHide', function() {
3838
$rootScope.$digest();
3939
expect(element).toBeShown();
4040
}));
41+
42+
43+
it('should follow javascript `truthy`/`falsy` logic', inject(function($rootScope, $compile) {
44+
var cases = ['[]', 'f', [], [''], 'false', {}, function() {}, function(f) {}, 0, false, null, undefined, '', NaN];
45+
element = jqLite('<div ng-show="exp"></div>');
46+
element = $compile(element)($rootScope);
47+
angular.forEach(cases, function(value) {
48+
$rootScope.exp = value;
49+
$rootScope.$digest();
50+
expect(element)[value ? 'toBeShown' : 'toBeHidden']();
51+
});
52+
}));
4153
});
4254

4355
describe('ngHide', function() {
@@ -49,6 +61,18 @@ describe('ngShow / ngHide', function() {
4961
$rootScope.$digest();
5062
expect(element).toBeHidden();
5163
}));
64+
65+
66+
it('should follow javascript `truthy`/`falsy` logic', inject(function($rootScope, $compile) {
67+
var cases = ['[]', 'f', [], [''], 'false', {}, function() {}, function(f) {}, 0, false, null, undefined, '', NaN];
68+
element = jqLite('<div ng-hide="exp"></div>');
69+
element = $compile(element)($rootScope);
70+
angular.forEach(cases, function(value) {
71+
$rootScope.exp = value;
72+
$rootScope.$digest();
73+
expect(element)[value ? 'toBeHidden' : 'toBeShown']();
74+
});
75+
}));
5276
});
5377
});
5478

0 commit comments

Comments
 (0)
Please sign in to comment.