0

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.

0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Would I then just “return value” from the function being called in thr ngIf?
Yeah you can do that too
Thank you for the recommendation. I'm finding typescript to be a little complicated, still learning, but thanks again for taking time to recommend a direction.
Glad to help you
Hi@Sajeetharan, I think you are wrong here, You said -" You cannot use indexOf with *ngIf on the template." it's not correct, We can use indexOf() in the template. check it by placing it in the template, Its works. <img src="wallpaperbrowse.com/media/images/hd-wallpapers-1.jpg" *ngIf="selectedimage.indexOf(v) !== -1">.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.