javascripthtmlangularjsangularjs-ng-repeatng-hide

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.


Solution

  • 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)"