角度打字稿的indexOf [英] indexOf for angular typescript
问题描述
我正在尝试编写类似的 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屋!