0

I have an array that I am iterating through and displaying. Sometimes, however, the item it finds is an array. I'd like to do something different with this, using an ng-if. I am unsure how to do this easily. It seems like a common issue but there doesn't appear to be an easy solution.

Meep3D
  • 3,803
  • 4
  • 36
  • 55

1 Answers1

2

You can use Angular.isArray() (see doc) but it does not exist as a already defined filter so you might need to define your own.

Something like that :

angular.module('...', []).filter('isArray', function() {
  return function (input) {
    return angular.isArray(input);
  };
});

And then in your template usimply use {{ myVar | isArray }}.

My only concerne is ... is it really clean to do so ? I don't know but this will solve your problem.

By the way, was already asked on StakcOverFlow : Angular expression to check if model is array or object

Community
  • 1
  • 1
sebastienbarbier
  • 6,542
  • 3
  • 29
  • 55