Verify empty object

0

I have the following code:

 <div ng-if="personas.hijos">
    Tiene hijos
 </div>

 <div ng-if="!personas.hijos">
    No tiene hijos
 </div>

 {{personas.hijos}}  => 

1 - si tiene me devuelve el json
{"test":{"a":0.8625,"b":0.5},"test2":{"a":0.72,"b":0.5}} 

2 - si no tiene me pinta {}

The problem is that he always enters the first div, he has children

    
asked by sirdaiz 24.11.2017 в 11:22
source

1 answer

1

Try adding a function to the controller:

$scope.isObjectEmpty = function(obj) {
  return !!Object.keys(obj).length;
}

and then call it in the view:

 <div ng-if="isObjectEmpty(personas.hijos)">
    Tiene hijos
 </div>

 <div ng-if="!isObjectEmpty(personas.hijos)">
    No tiene hijos
 </div>

It will always enter the first view because even if it has or does not have data personas.hijos is an object, and if you convert an object to boolean it will be true :

if ({}) {
  console.log('siempre entrare');
}
    
answered by 24.11.2017 в 11:59