Angular 2-ngFor-局部变量"first"不起作用 [英] Angular 2 - ngFor - local variable "first" does not work

查看:54
本文介绍了Angular 2-ngFor-局部变量"first"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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>

我在这里有一个有效的示例柱塞预览

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.

推荐答案

由于本地变量first不存在,所以现在可以使用[disabled]="i === 0",但是有一个

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.

更新

上面的拉取请求参考随beta.15一起发布,您可以看到更改日志 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆