onclick add active class in angular

Solutions on MaxInterview for onclick add active class in angular by the best coders in the world

showing results for - "onclick add active class in angular"
Louisa
08 Feb 2016
1You can do something like:
2
3<ul class="sub_modules">
4  <li (click)="activateClass(subModule)"
5      *ngFor="let subModule of subModules"
6      [ngClass]="{'active': subModule.active}">
7    <a>{{ subModule.name }}</a>
8  </li>
9</ul>
10On The component
11
12activateClass(subModule){
13  subModule.active = !subModule.active;    
14}
15On the Ng class the first property is the class you wanna add and the second is the condition;
Stefania
20 May 2018
1// if needed add a boolean to the array and use it
2for(let i = 0; i < this.subModules.length; i++){
3	this.subModules[i].active = false;
4}
5
6<ul class="sub_modules">
7  <li (click)="activateClass(subModule)"
8      *ngFor="let subModule of subModules"
9      [ngClass]="{'active': subModule.active}">
10    <a>{{ subModule.name }}</a>
11  </li>
12</ul>
13On The component
14
15activateClass(subModule){
16  subModule.active = !subModule.active;    
17}