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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book