Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide/show individual items inside ngFor

Tags:

I need to show / hide part of component. Here is Angular2 example.

<li *ngFor=" #item of items " >   <a href="#" (onclick)="hideme = !hideme">Click</a>   <div [hidden]="hideme">Hide</div> </li> 

Suppose we have only 2 items. Problem here that it works on both items. So it hides and shows div part in both components. It could be perfect if we could have something like this:

<li *ngFor=" #item of items " >    <a href="#" (onclick)="this.hideme = !this.hideme">Click</a>    <div [hidden]="this.hideme">Hide</div> </li> 

So is there some simple way to solve this problem ?

like image 439
YakovlevRoman Avatar asked Apr 26 '16 19:04

YakovlevRoman


People also ask

Why * is used in ngFor?

In *ngFor the * is a shorthand for using the new angular template syntax with a template tag, this is also called structural Directive.It is helpful to know that * is just a shorthand to explicitly defining the data bindings on a template tag.

What is * in ngFor Angular?

*ngFor is a predefined directive in Angular. It accepts an array to iterate data over atemplate to replicate the template with different data. It's the same as the forEach() method in JavaScript, which also iterates over an array.

Can we use ngFor for object?

You use the *ngFor directive to traverse over an array object and display the data in the UI.

How can I use two ngFor in one div?

You can't use multiple *ngFor s in the same element as those are structural directives and angular handles them in a specific way (basically for each structural directive it creates an ng-template element which then holds the directive itself, you can read more about it here: https://angular.io/guide/structural- ...


2 Answers

You're hideme variable is global. Perhaps you could attach it to the current item:

<li *ngFor=" #item of items " >   <a href="#" (onclick)="item.hideme = !item.hideme">Click</a>   <div [hidden]="item.hideme">Hide</div> </li> 

Otherwise you need to use a dedicated object object from your component. Here is a sample:

<li *ngFor="let item of items " >   <a href="#" (onclick)="hideme[item.id] = !hideme[item.id]">Click</a>   <div [hidden]="hideme[item.id]">Hide</div> </li> 

Don't forget to initialize the hideme object this way in your component:

hideme:<any> = {}; 

Edit

If you want to make this work like tabs, you need a bit more work ;-)

<li *ngFor="let item of items " >   <a href="#" (onclick)="onClick(item)">Click</a>   <div [hidden]="hideme[item.id]">Hide</div> </li> 

And to display the clicked element and hide others:

onClick(item) {   Object.keys(this.hideme).forEach(h => {     this.hideme[h] = false;   });   this.hideme[item.id] = true; } 
like image 168
Thierry Templier Avatar answered Sep 25 '22 15:09

Thierry Templier


is work for me :

in your compoment decler about hideme as array

hideme=[] 

and in the ngFor do that:

<li *ngFor="item of items;let i = index " >    <a (click)="hideme[i] = !hideme[i]">show/hide</a>    <div [hidden]="hideme[i]">The content will show/hide</div> </li> 
like image 20
yanai edri Avatar answered Sep 25 '22 15:09

yanai edri