angularjs default text for empty ng-repeat on javascript object

angularjs, javascript

Solution

You are correct, there is no object.length in javascript. Because you are resetting sources to an empty object, you need to check to see if the object is empty, or has some sources. You can write a simple function called isEmpty.

   <div ng-show="isEmpty(sources)">
          EMPTY SOURCES
   </div>

function Ctrl1($scope) {
    $scope.sources = {source1:{id:"source1"},source2:{id:"source2"}};

    $scope.cleanSources = function(){
        $scope.sources = {};                               
    };

    $scope.isEmpty = function (obj) {
       return angular.equals({},obj); 
    };
}

http://jsfiddle.net/zHJv8/21/

EDIT: Changed isEmpty to use angular.equals instead of for each loop.

Problem

I have a problem getting default text to print out for an empty ng-repeat that is iterating over a javascript object. Normally I would just do a `<div ng-show="object.length==0">EMPTY SET</div>"` but you can't call length on a javascript object. I have a simple jsfiddle to demonstrate the problem: http://jsfiddle.net/C4t4LystX/zHJv8/8/. Basically I just need to know when the ng-repeat has no objects to repeat over so I can show some default text. Thanks for any help.

Original source