Angular 2 - ngFor - local variable “first” does no

2019-06-20 14:36发布

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.

1条回答
该账号已被封号
2楼-- · 2019-06-20 15:30

For now you can use [disabled]="i === 0" since the local variable first 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.

查看更多
登录 后发表回答