U
U
ummahusla2015-06-22 11:48:37
Angular
ummahusla, 2015-06-22 11:48:37

How to separate ng-repeat with commas?

I have a book object, this object has an array of genres.

var book = { name:'Game Of Thrones', price: 19.95, author:'George R. R. Martin', genres:['Fantasy', 'Political strategy', 'High fantasy']};

When I try to display the entire date from genres using ng-repeat, it doesn’t work, it’s nice to separate this whole thing with commas.
<p>Genres: <strong ng-repeat="genre in store.product.genres">{{genre}}</strong></p>

Been through:
<p>Genres: <strong ng-repeat="genre in store.product.genres">{{genre ? '' : ', '}}</strong></p>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-06-22
Protko @Fesor

Yes, what kind of people then went ... If you can do something in the templates, then everyone is trying to do it there.

angular.module('app').filter('join', function () {
    return function (arr, glue) {
         if (!Array.isArray(arr)) {return arr;}
         return arr.join(glue);
    };
});

<strong>{{store.product.genres | join : ', '}}</strong>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question