I am using ngFor loop to create a list with buttons to move objects around. I have attempted to use the ngFor variables first and last to disable certain buttons. I am finding "first" does not work
<ul>
<li *ngFor="#hero of heroes; #i=index, #first=first, #last=last">
<button class="btn btn-default btn-lg" [disabled]="first" (click)="moveToTop(hero, i)">Top</button>
<button class="btn btn-default btn-lg" [disabled]="first" (click)="moveUp(hero, i)">Up</button>
<button class="btn btn-default btn-lg" [disabled]="last" (click)="moveDown(hero, i)">Down</button>
<button class="btn btn-default btn-lg" [disabled]="last" (click)="moveToBottom(hero, i)">Bottom</button>
</li>
I have a working example here Plunker preview
Am I doing this correctly? I know I could do
[disabled]="i==0"
but I was thinking "first" and "last" looked more elegant.
For now you can use
[disabled]="i === 0"
since the local variablefirst
doesn't exist, but there's a pull request to add it, not yet mergerd though.Update
The pull request reference above it landed with beta.15, you can see the changelog https://github.com/angular/angular/blob/master/CHANGELOG.md.
Here's a plnkr with
first
working. You can see the documentation as well.