简体   繁体   English

ngIf 数组包含索引

[英]ngIf array contains index

I have the following code:我有以下代码:

  <li *ngFor="let item of serviceList; let i = index;">
       <button *ngIf="currentTools contains i" (click)="processService(item)">
       Run Service</button>
  </li>

Is there any way in Angular 2 to check if an array contains the index of my item? Angular 2 中是否有任何方法可以检查数组是否包含我的项目的索引?

I basically only want to show the items when their index number is in an array (currentTools), which is dynamically modified in a different process.我基本上只想在它们的索引号位于数组(currentTools)中时显示这些项目,该数组在不同的进程中动态修改。

<li *ngFor="let item of serviceList; let i = index;">
  <button *ngIf="currentTools.indexOf(i)>-1" (click)="processService(item)">
    Run Service
  </button>
</li>

Try this 尝试这个

You need to avoid function call inside a data binding like *ngIf .您需要避免在*ngIf Taking this in to account, you should probably do this考虑到这一点,您可能应该这样做

 <li *ngFor="let item of serviceList; let i = index;">
    <ng-container *ngFor=let currentTool of currentTools>
       <button *ngIf="currentTool === i" (click)="processService(item)">
       Run Service</button>
    </ng-container>
  </li>

You can get more info about why not tu call function inside data binding here Don't Use Functions Inside Angular Templates and What to Use Instead您可以在此处获取有关为什么不在数据绑定中调用 function 的更多信息不要在 Angular 模板中使用函数以及改用什么

Also, you can check a similar answer here此外,您可以在此处查看类似的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM