William Boman
William Boman

Reputation: 2227

Pass the return value of a function to directive - AngularJS

Let's say I have a directive myDirective. That directive wants to have an array of tag names tags, like for example ['new', 'owner']. This array is generated on the fly like;

<my-directive ng-repeat="that in a" tags="getTags(that.id)"></my-directive>

angular.module('x').directive(...
  scope: {
   tags: '@'
  }
};

Where getTags is a function that returns an array of tags.

Like this, tags will just become a string "getTags(that.id)". If I put it in an expression like;

<my-directive ng-repeat="that in a" tags="{{getTags(that.id)}}"></my-directive>

tags will still be a string. But it will look like "['new', 'owner']" - but still a string. How do I pass the array?

Upvotes: 1

Views: 946

Answers (1)

Wawy
Wawy

Reputation: 6269

You need to parse the variable:

scope.tags = scope.$eval(scope.tags);

Upvotes: 3

Related Questions