goWithSwagger
goWithSwagger

Reputation: 1772

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?

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.

Upvotes: 1

Views: 7554

Answers (2)

Ernesto Alfonso
Ernesto Alfonso

Reputation: 725

You need to avoid function call inside a data binding like *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

Also, you can check a similar answer here

Upvotes: 0

wessam yaacob
wessam yaacob

Reputation: 937

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

Try this

Upvotes: 5

Related Questions