Open In App

AngularJS angular.isDefined() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The angular.isDefined() function in AngularJS is used to determine the value inside isDefined function is defined or not. It returns true if the reference is defined otherwise returns false

Syntax:

angular.isDefined( value );

Parameter value:

  • value: This parameter is a reference to check whether the entered value is defined or not.

Return Value: It returns true if the passed value is defined otherwise returns false

Example: This example uses angular.isDefined() function to determine the value inside isDefined function is defined or not. 

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>angular.isDefined()</title>
    <script src=
"//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">
        GeeksforGeeks
    </h1>
    <h3>angular.isDefined()</h3>
    <div ng-controller="geek">
        <b>Date:</b> {{date}}
        <br><br> {{isDefined}}
    </div>
      
    <!-- Script to uses angular.isDefined() function -->
    <script>
        var app = angular.module("app", []);
        app.controller('geek', ['$scope', function($scope) {
            $scope.date;
            $scope.isDefined =
            angular.isDefined($scope.date) == true ? 
            "$scope.date is defined." : "$scope.date is undefined.";
        }]);
    </script>
</body>
</html>


Output:

  • Date is not Defined:

isDefined

  • If Date is defined and its value is “2019-04-07T23:46:20.586”:

isDate

Example 2: This example describes the usage of angular.isDefined() Function in AngularJS by specifying the current time.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>angular.isDefined()</title>
    <script src=
"//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">GeeksforGeeks</h1>
    <h3>angular.isDefined()</h3>
    <div ng-controller="geek"
        <b>Time:</b> {{ today| date : 'mediumTime'}}
        <br /><br /> {{isDefined}}
    </div>
      
    <!-- Script to uses angular.isDefined() function -->
    <script>
        var app = angular.module('app', []);
        app.controller('geek', ['$scope',
            function($scope) {
                $scope.today = new Date();
                $scope.isDefined = 
                angular.isDefined($scope.today) == true ?
                '$scope.date is defined.' : '$scope.date is undefined.';
            },
        ]);
    </script>
</body>
</html>


Output:

  • Time is not Defined:

 

  • When Time is defined:

 



Last Updated : 06 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads