Angular ngClass and click event for toggling class
Asked Answered
E

9

88

In Angular, I would like to use ngClass and click event to toggle class. I looked through online but some are angular1 and there isn't any clear instruction or example. Any help will be much appreciated!

In HTML, I have the following:

<div class="my_class" (click)="clickEvent($event)" ngClass="{'active': toggle}">
  Some content
</div>

In .ts:

clickEvent(event) {
  // Haven't really got far
  var targetEle = event.srcElement.attributes.class;
}
Eagle answered 14/6, 2017 at 4:35 Comment(0)
D
161

This should work for you.

In .html:

<div class="my_class" (click)="clickEvent()"  
    [ngClass]="status ? 'success' : 'danger'">                
    Some content
</div>

In .ts:

status: boolean = false;
clickEvent(){
    this.status = !this.status;       
}
Diplocardiac answered 14/6, 2017 at 4:41 Comment(2)
Just a quick question. The first click adds the class just fine, but the second click doesn't seem to work. From your code, I don't see how it changes the status back to false.Eagle
made changes to my code., you can set it to false initially and then the function will toggle the bool value to true<-> false.Diplocardiac
C
76

Instead of having to create a function in the ts file you can toggle a variable from the template itself. You can then use the variable to apply a specific class to the element. Like so-

component.html -

<div (click)="status=!status"  
    [ngClass]="status ? 'success' : 'danger'">                
    Some content
</div>

So when status is true the class success is applied. When it is false danger class is applied.

This will work without any additional code in the ts file.
EDIT: Recent versions of angular require the variable to be declared in the controller - component.ts -

status: boolean = false;
Chopstick answered 30/1, 2018 at 11:52 Comment(5)
Would you have to declare "status" in the template?Albumenize
@Albumenize no, you do not need to declare status in the template.Meliamelic
Is it possible to use this approach also for a div inside an ngFor ? Because I try it, but it seems that angular change the class to all the div created with ngFor, not only to the one that I click; thanks.Virgule
Getting error that the - Property 'status' does not exist on type <Component Name>Lemons
@Lemons looks like things have changed in angular recently so you'll have to declare the variable first. I've updated the answer above.Chopstick
M
43

Angular6 using the renderer2 without any variables and a clean template:

template:

<div (click)="toggleClass($event,'testClass')"></div>

in ts:

toggleClass(event: any, className: string) {
    const hasClass = event.target.classList.contains(className);

    if (hasClass) {
        this.renderer.removeClass(event.target, className);
    } else {
        this.renderer.addClass(event.target, className);
    }
}

One could put this in a directive too ;)

Milzie answered 17/8, 2018 at 10:44 Comment(3)
Should not use reserved keyword class.Knorring
this solution not work in my caseKhoury
What is the issue on your side? Maybe I can help : )Milzie
P
16

ngClass should be wrapped in square brackets as this is a property binding. Try this:

<div class="my_class" (click)="clickEvent($event)"  [ngClass]="{'active': toggle}">                
    Some content
</div>

In your component:

//define the toogle property
private toggle : boolean = false;
              
//define your method
clickEvent(event){
   //if you just want to toggle the class; change toggle variable.
   this.toggle = !this.toggle;       
}

Hope that helps.

Pincer answered 14/6, 2017 at 4:40 Comment(0)
I
13

If you're looking for an HTML only way of doing this in angular...

<div #myDiv class="my_class" (click)="myDiv.classList.toggle('active')">
  Some content
</div>

The important bit is the #myDiv part.

It's a HTML Node reference, so you can use that variable as if it was assigned to document.querySelector('.my_class')

NOTE: this variable is scope specific, so you can use it in *ngFor statements

Infest answered 14/2, 2021 at 22:26 Comment(0)
E
3

We can also use ngClass to assign multiple CSS classes based on multiple conditions as below:

<div
  [ngClass]="{
  'class-name': trueCondition,
  'other-class': !trueCondition
}"
></div>
Equilateral answered 19/6, 2019 at 7:17 Comment(0)
L
1

If you want to toggle text with a toggle button.

HTMLfile which is using bootstrap:

<input class="btn" (click)="muteStream()"  type="button"
          [ngClass]="status ? 'btn-success' : 'btn-danger'" 
          [value]="status ? 'unmute' : 'mute'"/>

TS file:

muteStream() {
   this.status = !this.status;
}
Logotype answered 29/6, 2018 at 11:17 Comment(0)
B
1

So normally you would create a backing variable in the class and toggle it on click and tie a class binding to the variable. Something like:

@Component(
    selector:'foo',
    template:`<a (click)="onClick()"
                         [class.selected]="wasClicked">Link</a>
    `)
export class MyComponent {
    wasClicked = false;

    onClick() {
        this.wasClicked= !this.wasClicked;
    }
}
Blighter answered 18/11, 2019 at 3:35 Comment(0)
L
1

You can try this.

Html.

<button *ngFor="let color of colors; let index=index" class="example1" 
        (click)="selectColor(index)" [class.choose__color]="viewMode == index">
    <mat-icon>fiber_manual_record</mat-icon>
</button>

css.

.example1:hover {
    border-bottom: 2px solid black;
}

.choose__color {
     border-bottom: 2px solid black;
}

ts.

selectColor(index: any) {
    this.viewMode = index;
}
Lehet answered 8/7, 2021 at 21:56 Comment(1)
How to unset color on component blur (unselect)?Alterative

© 2022 - 2024 — McMap. All rights reserved.