epg388
epg388

Reputation: 137

ng-repeat don't show last piece of text

I have a list of answers that I would like to separate by a comma, but I do not want the last comma to show - how can I do this using ng-repeat and !$last? This is the html I have so far (which is not showing the entire last answer):

<h3 ng-repeat="answer in correctAnswers" ng-show="!$last">
    {{answer + "," + " " }}
</h3>

Upvotes: 1

Views: 180

Answers (2)

Brocco
Brocco

Reputation: 64853

I would recommend not using a separate span and toggling visibility, but use something closer to what you have already attempted:

 <h3 ng-repeat="answer in correctAnswers">
    {{answer + ($last ? '' : ',')}}
</h3>

No extra directives to be processed, just simple boolean logic w/ concatenation of a string

Upvotes: 2

xiaoboa
xiaoboa

Reputation: 1953

For convenient, you can try this

<h3 ng-repeat="answer in correctAnswers">
    {{answer}}<span ng-show="!$last"> + "," + " "<span>
</h3>

Upvotes: 2

Related Questions