AngularJs check empty array


AngularJs check empty array- It is very simple to check if an array is empty in AngularJs. You can just use the array.length property. Here in this tutorial we are going to explain how you can check if an array is empty. You can use our online demo try and edit the code online.


AngularJs check empty array | Example

array.length property is used to find the array length ie. size of array. It will give 0 if array is having no element.

AngularJs check empty array | 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,$timeout) {
 $scope.myArray = [];
 $scope.checkArray = function() {
	 if($scope.myArray.length > 0){
		 // do stuffs
	 }else{
		 alert('Given array is empty!');
	 }
 };
});
</script>
</head>
<body>  
<div ng-app="myApp">  
<div ng-controller="myController">  
 
 <button ng-click='checkArray()' >Check Array</button>

</div> 
</div>
</body>  
</html>      

Try it »

If you run the above example it will produce the output something like this –

AngularJs set variable in view


Advertisements

Add Comment

📖 Read More