问题
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