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
vm.isEmpty = function(obj) { return Object.keys(obj).length === 0; }
. Then you can useng-if="vm.isEmpty(vm.testObject)"
– Karolynkaron