indexOf 用于角度打字稿 [英] indexOf for angular typescript

查看:36
本文介绍了indexOf 用于角度打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个类似的 true/false 语句,类似于 .selected() 方法在 角度打字稿.

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

这个想法是在计算或 *ngIf 语句的计算结果为 True 时显示图像.代码写在app.html

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

代码:

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

在这段代码之前有一个 *ngFor 语句,以 ;let z = index 结尾.整个代码根据数组中存在的元素数量动态创建行数.(此处未提供代码.)然后,如果用户单击 href 链接,则索引值将传递到一个方法中,该方法将其推送到一个数组中.在 console.log(this.selectedimage) 中,每次单击 href 引用时,我都可以看到添加的值.不知道为什么 ngIf 逻辑不起作用.

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.

非常感谢所有帮助.

推荐答案

您不能在模板上使用 indexOf 和 *ngIf,我建议您创建一个基于逻辑返回 true/false 的函数.在函数中使用 indexOf 并使用 *ngIf

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

这篇关于indexOf 用于角度打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆