I need to create a comma-separated list of items:
<li ng-repeat="friend in friends">
<b ng-repeat="email in friend.email">{{email}}{{$last ? '' : ', '}}</b>...
</li>
According to the AngularJS documentation, no control flow statements is allowed in expressions. This is why my {{$last ? '' : ', '}}
does not work.
Is there an alternative way to create comma-separated lists?
EDIT 1
is there something simpler than:
<span ng-show="!$last">, </span>
You could do it this way:
<b ng-repeat="email in friend.email">{{email}}{{$last ? '' : ', '}}</b>
..But I like Philipp's answer :-)
Just use Javascript's built-in join(separator)
function for arrays:
<li ng-repeat="friend in friends">
<b>{{friend.email.join(', ')}}</b>...
</li>
Also:
angular.module('App.filters', [])
.filter('joinBy', function () {
return function (input,delimiter) {
return (input || []).join(delimiter || ',');
};
});
And in template:
{{ itemsArray | joinBy:',' }}
You can use CSS to fix it too
<div class="some-container">
[ <span ng-repeat="something in somethings">{{something}}<span class="list-comma">, </span></span> ]
</div>
.some-container span:last-child .list-comma{
display: none;
}
But Andy Joslin's answer is best
Edit: I changed my mind I had to do this recently and I ended up going with a join filter.
.list-comma::before {
content: ',';
}
.list-comma:first-child::before {
content: '';
}
<span class="list-comma" ng-repeat="destination in destinations">
{{destination.name}}
</span>