0
I own a property elemento$
inside my component, whose type is Observable<Objeto[]>
. I used the directive *ngFor
to access the data individually and show them on the screen:
<ul>
<li *ngFor="let elemento of elemento$ | async">
<p>{{elemento.mensagem}}</p>
</li>
</ul>
When performing the activity, I had to insert a tag that should not appear when I access the first element of the array, which I solved so:
<ul>
<li *ngFor="let elemento of elemento$ | async; let i = index">
<div *ngIf="!!i">isto não deve aparecer no primeiro elemento</div>
<p>{{elemento.mensagem}}</p>
</li>
</ul>
At the end of this, I also had to create a field that cannot be visible to the last element of the loop:
<ul>
<li *ngFor="let elemento of elemento$ | async; let i = index">
<div *ngIf="!!i">isto não deve aparecer no primeiro elemento</div>
<p>{{elemento.mensagem}}</p>
<div>isto não deve aparecer no último elemento</div>
</li>
</ul>
To solve this, I used the following code:
<div *ngIf="i != ((elemento$ | async).length - 1)">isto não deve aparecer no último elemento</div>
But I don’t think it’s the ideal way to do it. There’s a way to do it using the properties of the directive itself *ngFor
(ex: *ngfor="let x of y; let total = length"
)?
Confused this huh man!!
– LeAndrade
Gambi design Patterns
– Arthur Siqueira