Check object size in AngularJS template, doing it wrong? Check object size in AngularJS template, doing it wrong? angularjs angularjs

Check object size in AngularJS template, doing it wrong?


You can use Object in ngIf directive everywhere. Just put its reference to the root scope:

app.run(function($rootScope) {    $rootScope.Object = Object;});

It will also be available in isolated scopes such as in directives.

UPD. In recent version of Angular you can't reference Object in expressions anymore. In this case you will need to reference methods individually:

app.run(function($rootScope) {    $rootScope.keys = Object.keys;});

and then use this function:

ng-if="keys(myObject).length"


In your controller, add this line

$scope.Object = Object;

Then you should be able to use Object.XXX in your template like what you did.


Shouldn't this be sufficient?

<ul ng-if="myObject">  <li ng-repeat="(key, val) in myObject">      Value at key {{ key }} is: {{ val }}  </li></ul>

The directive takes care of null, undefined, empty objects. Plunkr for demo. Try modifying object to undefined, null or populate with value.