typescript *ng如果数组中存在值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51140214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
*ngIf value exists in array
提问by Vlad N
I have a question regarding the ngIf directive.
我有一个关于 ngIf 指令的问题。
I have some angular material toggles in my application that are being generated dynamically, all of them have a specific id. I was wondering, I know that using the ngIf directive I can conditionally show those elements on my page. I am using ngIf for other functions but I am a bit lost on how I might be able to check if the id of the element exists in an array.
我的应用程序中有一些动态生成的角度材质切换,它们都有一个特定的 ID。我想知道,我知道使用 ngIf 指令可以有条件地在我的页面上显示这些元素。我正在将 ngIf 用于其他函数,但我对如何检查元素的 id 是否存在于数组中有点迷茫。
The array looks something like this:
数组看起来像这样:
["fc-vis", "fc-bis", "fc-dis"]
and my toggles all have the id: fc-vis / fc-bis ... and so on.
和我的切换都有 id:fc-vis / fc-bis ... 等等。
I've tried using ngIf but I don't know how to send the id of the element in the condition... Or I might be doing something wrong
我试过使用 ngIf 但我不知道如何在条件中发送元素的 id ......或者我可能做错了什么
What I am trying to do is something like :
我想要做的是:
*ngIf="object.id in myArrayList"
or
或者
*ngIf="checkIfExists(object.id)"
Any idea on how I could use the object's id in the condition, to check that it exists in that array ?
关于如何在条件中使用对象的 id 来检查它是否存在于该数组中的任何想法?
回答by pala?н
If you support modern browsers, then you can use Array
includes()
method here like:
如果您支持现代浏览器,那么您可以Array
includes()
在此处使用方法,例如:
*ngIf="myArrayList.includes(object.id)"
- The
includes()
method determines whether an array includes a certain value among its entries, returning true or false as appropriate. Thus there will be no need to do additional checks here after calling this method.
- 该
includes()
方法确定数组是否在其条目中包含某个值,并根据需要返回 true 或 false。因此,调用此方法后无需在此处进行额外检查。
For supporting IE as well, you can use Array
indexOf()
method here like:
为了支持 IE,您可以Array
indexOf()
在此处使用方法,例如:
*ngIf="myArrayList.indexOf(object.id) > -1"
- The
indexOf()
method returns the first index at which a given element can be found in the array, or -1 if it is not present. Thus after calling this method we will also need to do an additional check to see if the value returned fromindexOf()
method is greater than-1
or not and based on that*ngIf
will show or hide the element.
- 该
indexOf()
方法返回可以在数组中找到给定元素的第一个索引,如果不存在,则返回 -1。因此,在调用此方法后,我们还需要进行额外的检查,以查看从indexOf()
方法返回的值是否大于-1
或不大于,并基于此*ngIf
显示或隐藏元素。
回答by Avinash
You can also use Array indexOf()method, it returns -1 if the element is not there in the array:
in your ts - const myArray = ["fc-vis", "fc-bis", "fc-dis"]
in template - *ngIf="myArray.indexOf(object.id) > -1"
use templateRef to get the id of the element, example:
您还可以使用 Array indexOf()方法,如果元素不在数组中,则返回 -1:
在您的 ts -const myArray = ["fc-vis", "fc-bis", "fc-dis"]
在模板中 -*ngIf="myArray.indexOf(object.id) > -1"
使用 templateRef 获取元素的 id,例如:
<div id="fc-vis" #myId>
<p *ngIf="myArray.indexOf(myId?.id) > -1">{{myId.id}} working </p>
</div>