Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

*ngIf value exists in array

I have a question regarding the ngIf directive.

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.

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.

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

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 ?

like image 733
Vlad N Avatar asked Jul 02 '18 16:07

Vlad N


People also ask

Can we use ngIf in Div?

On the div tag we added the ngIf directive that would be displayed if the value of toggleOn is false. After that, we added some dummy paragraphs. This is how to use the ngIf directive. It can be used in all types of use cases and comparisons you can think of in your component template.

What is * ngIf and how does it work?

NgIflink. A structural directive that conditionally includes a template based on the value of an expression coerced to Boolean. When the expression evaluates to true, Angular renders the template provided in a then clause, and when false or null, Angular renders the template provided in an optional else clause.

Why * ngIf is used?

The NgIf directive is used when you want to display or remove an element based on a condition. If the condition is false the element the directive is attached to will be removed from the DOM .


2 Answers

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:

<div id="fc-vis" #myId>
  <p *ngIf="myArray.indexOf(myId?.id) > -1">{{myId.id}} working </p> 
</div>
like image 21
Avinash Avatar answered Sep 18 '22 07:09

Avinash


If you support modern browsers, then you can use Array includes() method here like:

*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.

For supporting IE as well, you can use Array indexOf() method here like:

*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 from indexOf() method is greater than -1 or not and based on that *ngIf will show or hide the element.
like image 96
palaѕн Avatar answered Sep 21 '22 07:09

palaѕн