Nishant Roy Nishant Roy - 3 years ago 84
AngularJS Question

ng-repeat hide rows with empty object

I have a table where I am repeating over an array of objects. Within the object, there is a nested object, like so:

[
{"Object1":
{"Sub_obj" : {}
}
},

{"Object2":
{"Sub_obj" :
{"Name" : "Jane"}
}
}
]


I want to
ng-hide
the table row where
"Sub_obj"
is empty. How can I achieve this? I tried
ng-hide=!Sub_obj
, but that doesn't work.

Answer Source

Make a simple function that checks the length of the keys in object:

$scope.isEmptyObject(obj){
  return !angular.isObject(obj) || !Object.keys(obj).length
}

view

ng-hide="isEmptyObject(item.Sub_obj)"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download