brabertaser19 brabertaser19 - 4 months ago 42
Javascript Question

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?

Answer

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.

Comments