Test if an object is an empty object in a AngularJS template
Asked Answered
I

5

22

I have a simple object in a controller which can sometimes be empty ({}).

app.controller('TestController', function() {
  var vm = this;

  vm.testObject = {};
});

I want to hide or show some DOM-elements in the corresponding template when the object is empty or not.

I tried to do it with a simple <div ng-if="vm.testObject"> but when vm.testObject === {} it is considered true in the ng-if.

<div ng-controller="TestController as vm">
  <div ng-if="vm.testObject">
    Test Object is not empty
  </div>
  <div ng-if="!vm.testObject">
    Test Object is empty
  </div>
</div>

Is there a simple way to check for an empty object in the template? Preferably without adding new variables to the scope.

Here is a working Plunker: http://plnkr.co/edit/Qed2MKmuedcktGGqUNi0?p=preview

Ion answered 21/5, 2015 at 7:13 Comment(1)
How about a function, eg vm.isEmpty = function(obj) { return Object.keys(obj).length === 0; }. Then you can use ng-if="vm.isEmpty(vm.testObject)"Karolynkaron
F
24

Are you ok with moving the equality check to the ng-if?

<div ng-controller="TestController as vm">
  <div ng-if="!equals({}, vm.testObject)">
    Test Object is not empty
  </div>
  <div ng-if="equals({}, vm.testObject)">
    Test Object is empty
  </div>
</div>

Otherwise, provide a helper on the scope

app.controller('TestController', function() {
  var vm = this;

  vm.testObject = {};

  vm.empty = function() {
      return vm.testObject === {};
  };
});

then

<div ng-controller="TestController as vm">
  <div ng-if="!vm.empty()">
    Test Object is not empty
  </div>
  <div ng-if="vm.empty()">
    Test Object is empty
  </div>
</div>
Froufrou answered 21/5, 2015 at 7:21 Comment(2)
Does this helper even work? In Javascript {} === {} gives false.Houseboat
vm.empty = function(object) { return Object.keys(object).length === 0; };Plectognath
U
43

You should use an AngularJs filter:

Javascript:

app.filter('isEmpty', [function() {
  return function(object) {
    return angular.equals({}, object);
  }
}])

Html template:

<div ng-if="!(vm.testObject | isEmpty)">
  Test Object is not empty
</div>
<div ng-if="vm.testObject | isEmpty">
  Test Object is empty
</div>

Updated plunkr: http://plnkr.co/edit/J6H8VzUKnsNv1vSsRLfB?p=preview

Uhf answered 21/5, 2015 at 7:22 Comment(3)
You're right, thanks for helping me out there! Just corrected the answer.Uhf
For isNotEmpty return !angular.equals({}, object);Ravenravening
like this solution based on angular's framework instruments, i guess it is in a "canonical way" to archieve this goalDoublure
F
24

Are you ok with moving the equality check to the ng-if?

<div ng-controller="TestController as vm">
  <div ng-if="!equals({}, vm.testObject)">
    Test Object is not empty
  </div>
  <div ng-if="equals({}, vm.testObject)">
    Test Object is empty
  </div>
</div>

Otherwise, provide a helper on the scope

app.controller('TestController', function() {
  var vm = this;

  vm.testObject = {};

  vm.empty = function() {
      return vm.testObject === {};
  };
});

then

<div ng-controller="TestController as vm">
  <div ng-if="!vm.empty()">
    Test Object is not empty
  </div>
  <div ng-if="vm.empty()">
    Test Object is empty
  </div>
</div>
Froufrou answered 21/5, 2015 at 7:21 Comment(2)
Does this helper even work? In Javascript {} === {} gives false.Houseboat
vm.empty = function(object) { return Object.keys(object).length === 0; };Plectognath
C
1

This is an old thread but I find easier to check if the Object has keys.

<div ng-controller="TestController as vm">
  <div ng-if="Object.keys(vm.testObject).length">
    Test Object is not empty
  </div>
  <div ng-if="!Object.keys(vm.testObject).length">
    Test Object is empty
  </div>
</div>

It's simple and readable.

Colston answered 10/12, 2018 at 14:27 Comment(2)
Actually, this didn't work for me. Upvoted by mistake.Margheritamargi
it doens't works for me. Using version 1.3.1 (yeah, i know that is older..)Doublure
C
0

This will work. check the Length

<div ng-if="!!vm.testObject && vm.testObject.length > 0">
  Test Object is not empty
</div>
Competitor answered 15/6, 2017 at 12:0 Comment(0)
A
0

You can convert the object to a JSON string using the built-in AngularJS json filter and do a comparison like this:

<div ng-if="vm.testObject | json) != '{}'">
    Test Object is not empty
</div>
Aircraftman answered 7/10, 2021 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.