Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for an empty object in an AngularJS view

In the controller's scope I have something like:

$scope.card = {}; 

In the view I must check if my object is still an empty literal, {}, or if it contains some data in some fields.

I tried this:

ng-show="angular.equals({}, card)" 

and

ng-show="!angular.equals({}, card)" 

but it didn't work.

Are there any better ways? How do you check if an object is empty or if it contains some fields?

like image 352
brabertaser19 Avatar asked Sep 15 '15 12:09

brabertaser19


People also ask

How do you ask if an object is empty?

return Object.keys(obj).length === 0 ; This is typically the easiest way to determine if an object is empty.

How do I check if an array is empty in AngularJS?

We can also explicitly check if the array is empty or not. if (arr. length === 0) { console. log("Array is empty!") }


1 Answers

You can use: Object.keys(card).length === 0

But make sure you use it from a method of the controller as the Object is not available in the view, like:

$scope.isObjectEmpty = function(card){    return Object.keys(card).length === 0; } 

Then you can call the function from the view:

ng-show="!isObjectEmpty(card)" 
like image 140
Md Hasan Ibrahim Avatar answered Oct 05 '22 16:10

Md Hasan Ibrahim