Anyone knows how can I do something like this with Angular?
$('button').click(function(){
$('button').toggleClass('active');
$('.title').toggleClass('active');
$('nav').toggleClass('active');
});
Thank you very much!
You can use something like this([class.className]="conditionThatResolvesToABoolean", or [ngClass]) for doing this:
<div [class.active]="classApplied">
Here's some text
<button (click)="toggleClass()">Toggle Class</button>
</div>
And in your Template:
import { Component } from '@angular/core';
@Component({...})
export class AppComponent {
classApplied = false;
toggleClass() {
this.classApplied = !this.classApplied;
}
}
Here's a Sample StackBlitz for your ref.
This is the Typescript code to solve this problem
@Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit {
is_active = "";
constructor() { }
ngOnInit(): void {
}
toggleNav(){
if(this.is_active){
this.is_active = "";
}
else{
this.is_active = "is-active";
}
}
}
This is my way to do it now we can use is_active property in the html code
<div class="navbar-burger {{is_active}}" role="button" (click)="toggleNav()" data-target="navbarExampleTransparentExample">
ngClassangular.io/api/common/NgClass