ng-repeat not working for list of strings using custom directive

204 Views Asked by At

I am working on angular js project in which we are using ng-repeat to display list of strings in cell template created using custom directives.

It working fine for list of numbers. But it does not works for list of strings.

You can try by changing number to string on plunker

var viewFn = "<div>View: <span ng-repeat='x in [1,2,3]'>{{x}}</span></div>";
...
var editFn = "<div>Edit: <span ng-repeat='x in ["Hi","Hello"]'>{{x}}</span></div>";

How can I get list of string using cell template?

2

There are 2 best solutions below

0
On

Because editFn is a string, the quotes in your ng-repeat array need to be escaped.

Try: var editFn = "<div>Edit: <span ng-repeat='x in [\"Hi\",\"Hello\",\"Hey\"]'>{{x}}</span></div>";

1
On

The problem is the string character that you are using.

You can avoid it using template literals (backtick char).

Change your template using it like:

var editFn = `<div>Edit: <span ng-repeat="x in ['Hi','Hello']">{{x}}</span></div>`;