Angular 2 - ngFor - local variable “first” does not work

核能气质少年 提交于 2019-12-10 14:49:34

问题


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:


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.



来源:https://stackoverflow.com/questions/35856897/angular-2-ngfor-local-variable-first-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!