0

I've got an array of items in which I'm comparing a user id of the current element vs the previous one. When I try this code, it results in a blank page with no errors:

<ion-item *ngFor="let message of messages; let i = index;">
   <div *ngIf="message.user_id !== messages[i-1].user_id"></div>
</ion-item>

Can you not access array indexes in the template this way?

1
  • 3
    This example will probably fail for index [0] because [i-1] will be [-1] which doesn't exist Commented Aug 1, 2017 at 18:47

1 Answer 1

3

0-1 === -1 so skip the first item.

<ion-item *ngFor="let message of messages; let i = index; let isFirst = first">
   <div *ngIf="!isFirst && message.user_id !== messages[i-1].user_id"></div>
</ion-item>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.