Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a scope variable is undefined in AngularJS template?

How to check if a scope variable is undefined?

This does not work:

<p ng-show="foo == undefined">Show this if $scope.foo == undefined</p> 
like image 814
Sebastian Barth Avatar asked Nov 20 '14 15:11

Sebastian Barth


People also ask

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

What is $scope used for in angular?

The $scope in an AngularJS is a built-in object, which contains application data and methods. You can create properties to a $scope object inside a controller function and assign a value or function to it. The $scope is glue between a controller and view (HTML).

What is $scope in AngularJS?

$rootScope is a parent object of all “$scope” angular objects created in a webpage. $scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage. It is created with the ng-app directive. It is created with the ng-controller directive.

What is the difference between $scope and scope in AngularJS?

In short, in case of dependency injection the scope object is received as $scope while in case of non-dependency injection scope object is received as scope or with any name. Save this answer.


1 Answers

Here is the cleanest way to do this:

<p ng-show="{{foo === undefined}}">Show this if $scope.foo === undefined</p> 

No need to create a helper function in the controller!

like image 165
jlewkovich Avatar answered Sep 25 '22 23:09

jlewkovich