What is AngularJs isDefined() Function?

What is AngularJs isDefined() Function?

The angular isDefined() function is  use to check the value or references in defined or not in angular $scope.

If the value or reference is defined then return the true otherwise return false.

The live demo, click on link http://embed.plnkr.co/Cp2KA2/

Syntax :

                   angular.isDefined($val);      

The isDefined() function is case sensitive and takes a $val and returns true/false.

JavaScript Code :

       var app = angular.module('myApp', []);
        app.controller('myCtrl', ['$scope', function ($val) {
            $val.welcomeMsg = function () {
                if (angular.isDefined($val.message)) {
                    $val.lblmessage = 'Welcome you  ' + $val.message + '!';
                } else {
                    $val.lblmessage = 'Please enter your name';
                }
            }
        }]);

The example in detail as give below

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>AngularJSisDefined() Function</title>
    <script src="https://code.angularjs.org/1.4.9/angular.js"></script>
    <script>
        var app = angular.module('myApp', []);

        app.controller('myCtrl', ['$scope', function ($val) {

            $val.welcomeMsg = function () {
                if (angular.isDefined($val.message)) {
                    $val.lblmessage = 'Welcome you  ' + $val.message + '!';
                } else {
                    $val.lblmessage = 'Please enter your name';
                }
            }
        }]);
    </script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <div>
        <input type="text" ng-model="message" placeholder="Type a message" />
        <button ng-click="welcomeMsg()">Submit</button>
    </div>
    <h1>Result : {{lblmessage}}</h1>
</body>
</html>

Result : go for live demo http://embed.plnkr.co/Cp2KA2/





ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

What is AngularJs isDefined() Function? What is AngularJs isDefined() Function? Reviewed by Anil Singh on 11:47 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^