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" />
<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/