brabertaser19 brabertaser19 - 1 year ago 147
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)"


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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download