Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation of boolean expressions in AngularJS

I'm testing the ng-show and AngularJS expressions, but I found something I can't understand. I created a variable displayed and assigned a string 'false' (not boolean) to it. The expression displayed && true is evaluated to true, and the second div is shown without problem (because a string and true should be true). I know there are some differences between Angular expressions and JavaScript expressions, however I don't know why the first div is not shown; it seems that the displayed is compiled to a boolean by Angular.

Here is the jsfiddle: http://jsfiddle.net/micmia/1psf70tv/3/

Template:

<div ng-controller="MyCtrl">
    <div ng-show="displayed">test1</div>
    <div ng-show="displayed && true">test2</div>
</div>

Controller:

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function ($scope, $parse) {
    $scope.displayed = 'false';
});
like image 868
micmia Avatar asked May 21 '15 13:05

micmia


1 Answers

That's how angular evaluated the string 'false' and 'f' and some others as well. There was an open bug about it.

See this fiddle works with a later version.

like image 145
eladcon Avatar answered Sep 19 '22 10:09

eladcon