Angularjs check defined variable


Angularjs check defined variable : angular.isDefined() function is used to identify whether a variable is defined or not. Here in this tutorial we are going to explain how to check defined variables using the function angular.isDefined() in AngularJs. You can also use our online tool to run and edit the code.


Angularjs check defined variable | angular.isDefined | Example

To check a defined variable you can use angular.isDefined() function, it will return true if the variable is defined else it will return false. Here is an example of checking defined variable using angular.isDefined() function.

Angularjs check defined variable | angular.isDefined | Example:

<!DOCTYPE html>
<html lang="en">
<head>
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"></script>
 <script>
var myApp = angular.module("myApp", []);
 myApp.controller("myController", function($scope) {
  var a;
  var b = 10;
  $scope.result1 = '';
  $scope.result2 = '';
  $scope.chackDefinedVars = function() {
    if(angular.isDefined(a)){
		$scope.result1 = true;
	}else{
		$scope.result1 = false;
  }
  if(angular.isDefined(b)){
		$scope.result2 = true;
	}else{
		$scope.result2 = false;
  }
  }
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
    <button ng-click ="chackDefinedVars()" >Check Vars</button>
	<br>
	Result for a = {{result1}}<br>
	Result for b = {{result2}}
</div> 
</div>
</body>  
</html>                                                                                                                                                                                                                                                                                                                                                                                              

Try it »

After running the above example it will produce output something like this –

Angularjs check defined variable


Advertisements

Add Comment

📖 Read More