L1ghtk3ira L1ghtk3ira - 4 months ago 15
HTML Question

Angular JS repeat function 'peek' if there is another item in the array to be displayed

When I use ng-repeat I have a comma to split up information such as names in a nice row, however with the comma being in the

function it adds a comma at the end as well which is incorrect.

What I am thinking is there must be a way to check if there is another item to be displayed. If there is display the comma, and if there is not then don't display the comma.

An example of my code is as follows:

<tbody class="offerInformation">
<tr><td><b>Test:</b></td><td><span ng-repeat="testing in results.names"><span ng-bind="testing"></span> , </span></td></tr>

As you see the second table row has a comma that shows up for every repeated item in the array. Which in the end if there is say 3 people the output is as follows: Jason , Mark, Taylor ,

It leaves an extra comma which is incorrect. There should be no comma after the last item. My assumption is to use
to somehow check if there is another item coming next, if there is we display the comma but if there is not then we do not display the comma.

Any help is greatly appreciated.


ng-repeat directive has some built-in variables to get the context information. $last is one of them which you need in this case.

<span ng-repeat="testing in names">
      {{testing}}<span ng-if="!$last"> , </span>

Plunker example https://plnkr.co/edit/PjU6fqaXwUjp8PjY1Wr6?p=preview