Angular 2 - ngFor - 局部变量“first"不起作用 [英] Angular 2 - ngFor - local variable "first" does not work
问题描述
我正在使用 ngFor 循环来创建一个带有按钮的列表来移动对象.我试图首先和最后使用 ngFor 变量来禁用某些按钮.我发现第一"不起作用
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>
我在这里有一个工作示例 Plunker 预览
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.
推荐答案
现在你可以使用 [disabled]="i === 0"
因为局部变量 first
不存在,但有一个 pull request 来添加它,尚未合并尽管.
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.
更新
上面的pull request引用在beta.15登陆,可以看到changelog https://github.com/angular/angular/blob/master/CHANGELOG.md.
The pull request reference above it landed with beta.15, you can see the changelog https://github.com/angular/angular/blob/master/CHANGELOG.md.
这是一个 plnkr,first
正在运行.您也可以查看文档.
Here's a plnkr with first
working. You can see the documentation as well.
这篇关于Angular 2 - ngFor - 局部变量“first"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!