Skip to content

Commit

Permalink
fix(ngShow/ngHide, ngIf): functions with zero args should be truthy
Browse files Browse the repository at this point in the history
Previously, expressions that were a function with one or more arguments evaluated to
true, but functions with zero arguments evaluated to false.

This behavior seems both unintentional and undesirable. This patch makes a function
truthy regardless of its number of arguments.

Closes angular#5414
  • Loading branch information
btford authored and jamesdaily committed Jan 27, 2014
1 parent 53a8f48 commit b3874d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,9 @@ function fromJson(json) {


function toBoolean(value) {
if (value && value.length !== 0) {
if (typeof value === 'function') {
value = true;
} else if (value && value.length !== 0) {
var v = lowercase("" + value);
value = !(v == 'f' || v == '0' || v == 'false' || v == 'no' || v == 'n' || v == '[]');
} else {
Expand Down
10 changes: 10 additions & 0 deletions test/ng/directive/ngShowHideSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ describe('ngShow / ngHide', function() {
}));


// https://github.com/angular/angular.js/issues/5414
it('should show if the expression is a function with a no arguments', inject(function($rootScope, $compile) {
element = jqLite('<div ng-show="exp"></div>');
element = $compile(element)($rootScope);
$rootScope.exp = function(){};
$rootScope.$digest();
expect(element).toBeShown();
}));


it('should make hidden element visible', inject(function($rootScope, $compile) {
element = jqLite('<div class="ng-hide" ng-show="exp"></div>');
element = $compile(element)($rootScope);
Expand Down

0 comments on commit b3874d3

Please sign in to comment.