我有一个Angular 2应用程序,它在表格中显示名称列表,最右边的列显示一行图标,这些图标是用户可以选择的操作。通过选择编辑图标,将显示一个弹出窗口,允许用户编辑列表中的该条目。但是,对于某些名称,我想禁用该选项。除了创建第二个布尔值列表来隐藏和显示图标(Based on this SO answer)之外,我还能做些什么呢?我知道只有第一个元素不应该被编辑(因为它是为用户预先加载的),所以在链接中使用答案似乎是一个糟糕的解决方案。
发布于 2017-08-07 16:26:54
如果您知道它只描述了列表中的第一个元素,那么您可以在*ngFor
*ngFor
here in the documentation中使用index
选项
<div *ngFor="let hero of heroes; let i=index;">
({{i}}) {{hero.name}}
<ng-container *ngIf="i !== 0">
Show edit
</ng-container>
</div>
发布于 2017-08-09 13:35:49
0mpurdy的建议很棒,只是想引起注意,你也可以使用first
(这里借用0mpurdys的代码):
<div *ngFor="let hero of heroes; let first=first">
{{hero.name}}
<ng-container *ngIf="!first">
Show edit
</ng-container>
</div>
https://stackoverflow.com/questions/45551528
复制