indexOf for angular typescript

核能气质少年 提交于 2021-01-29 12:35:17

问题


I am trying to write a similar true/false statement, Similar to how the .selected() method would work in Angular typescript.

The idea is to show an image if the calculation or *ngIf statement evaluates to True. The code is written in the app.html side

Code:

<img src="/project/x.png" *ngIf="selectedimage.indexOf(v) !== -1"><a href="{{i['link']}}" target="blank" (click)="update_viewed(z)">

There is a *ngFor statement right before this code, with ;let z = index at the end. The overall code creates a # of rows dynamically depending on how many elements exist in an array. (code not provided for this here.) Then, if the user clicks on the href link, the index value is passed into a method, which pushes it to an array. in console.log(this.selectedimage) I can see the values being added in each time I click the href reference. Not sure why the ngIf logic isn't working.

All help much appreciated.


回答1:


You cannot use indexOf with *ngIf on the template, I would recommend you to create a function that returns true/false based on the logic. Use indexOf within the function and call it with *ngIf



来源:https://stackoverflow.com/questions/51296798/indexof-for-angular-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!