helloIs this an assessm...">

Does angularjs increment a null value like $ scope.null?

An example for a question

<div ng-show="bar !== null">hello</div> 

Is this an assessment in the field of how

 $scope.bar !== null 

or is that how?

 $scope.bar !== $scope.null 

Note that in the latter case, $ scope.null will be undefined, and the example will work correctly.

Bonus:

if bar = null then this will happen

 // this does not work (shows hello) <div ng-show="bar !== null">hello</div> 

does not give the same result as

 // this works ok (does not show hello) <div ng-show="bar != null">hello</div> 

Why is this happening?

+4
angularjs angularjs-scope
Jul 13 '15 at 15:27
source share
4 answers

No, Angular recognizes some JS primitives in expressions and null among them.

+3
Jul 13 '15 at 15:34
source share

Angular uses an advanced parser ( $ parse ) to process and evaluate attribute expressions as if they were JavaScript code. When it works, it will rank null a higher priority over the scope property with the same name. This means that in expressions like

 ng-show="bar !== null" 

Angular will indeed treat null as a primitive null value.

However, it is easy to instruct Angular that you want to use the scope property, not null primitive. In this case, you need to use parenthesis notation:

 ng-show="bar !== this['null']" 

And one last point. Although you can use properties with names like null , undefined , etc., I would not recommend it because it is confusing, and you need to use the detailed syntax of the brackets.

+6
Jul 13 '15 at 15:38
source share

null in angular expression is Javascript null not like $scope.null . It is easy to verify that using the expression evaluation example in https://docs.angularjs.org/guide/expression

I just tried entering 4 expressions:

 null=1 => null => x=1 => 1 x => 1 

and, as you can see, when you can set and access a variable, such as x , try to use null since the variable does not work in the evaluator of the angular expression.

+1
Jul 13 '15 at 15:35
source share

As estus says, Angular recognizes null .

If you want, you can do this in your controller: check if the value is null ( undefined false )

 var myApp = angular.module('myApp',[]); function MyCtrl($scope) { $scope.bar = 'notNull'; $scope.isNull = function(val) { return val === null; //If you want to check undefined as well, use : //return angular.isUndefined(val) || val === null; }; } <div ng-controller="MyCtrl"> <span ng-show="!isNull(bar)">Hello, {{bar}}!</span> </div> 
+1
Jul 13 '15 at 15:45
source share



All Articles