Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
AngularJS – isUndefined() method
The isUndefined() method in AngularJS checks if a reference is undefined or not. This method returns true if the reference passed to the function is undefined, otherwise it returns false.
Syntax
angular.isUndefined(value)
Parameters
- value ? The reference to check for undefined status
Return Value
Returns true if the value is undefined, false otherwise.
Example ? Check if References are Undefined
Create an HTML file in your project directory and copy the following code ?
<!DOCTYPE html>
<html>
<head>
<title>angular.isUndefined()</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js">
</script>
</head>
<body ng-app="app" style="text-align:center">
<h1 style="color:green">
Welcome to Tutorials Point
</h1>
<h2>AngularJS | angular.isUndefined()</h2>
<div ng-controller="example">
<b>Name: {{name}}</b>
<br><br>
{{isUndefinedResult}}
<br><br>
<b>Name2: {{name2}}</b>
<br><br>
{{isUndefinedResult2}}
</div>
<script>
var app = angular.module("app", []);
app.controller('example', ['$scope', function ($scope) {
// Defining variables
$scope.name = "SIMPLY LEARNING";
$scope.name2; // This is undefined
$scope.isUndefinedResult = angular.isUndefined($scope.name) == true
? "$scope.name is Undefined."
: "$scope.name is Defined.";
$scope.isUndefinedResult2 = angular.isUndefined($scope.name2) == true
? "$scope.name2 is Undefined."
: "$scope.name2 is Defined.";
}]);
</script>
</body>
</html>
Output
When you run the above code in a browser, you will see the following output ?
How It Works
In the example above:
-
$scope.nameis assigned the value "SIMPLY LEARNING", so it is defined -
$scope.name2is declared but not assigned any value, making it undefined - The
angular.isUndefined()method correctly identifies which variables are defined and which are not
Common Use Cases
The isUndefined() method is commonly used for:
- Form validation to check if required fields have values
- Conditional rendering based on data availability
- Default value assignment when properties are undefined
Conclusion
The angular.isUndefined() method is a useful utility for checking undefined references in AngularJS applications. It helps ensure proper handling of uninitialized variables and improves application reliability.
Advertisements
