2 Example(s) of angular isDefined


Description :

angular.isDefined checks whether variable is defined or not and if it is defined then return true otherwise return false. See the code snippet:


angular isDefined Example - 1
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isDefinedController">
        {{isDefined}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isDefinedController', ['$scope', function ($scope) {
        $scope.date = new Date;
        $scope.isDefined = angular.isDefined($scope.date) == true ? "$scope.date is defined." : "$scope.date is undefined.";
    }]);

</script>

Output

Description :

In this example of angular.isDefined, it return false as Obj does not have property Address so zit is undefined.


angular isDefined Example - 2
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Welcome in the AngularJS</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
</head>
<body ng-app="app">
    <div ng-controller="isDefinedController">
        {{isDefined}}
    </div>
</body>
</html>
<script>
    var app = angular.module("app", []);
    app.controller('isDefinedController', ['$scope', function ($scope) {
        var obj = {
            Name: "",
            Age: "",
        };
        $scope.isDefined = angular.isDefined(obj.Address);
    }]);
  
</script>

Output