I have a button, when clicked I would like the body to get the class "open-menu". With jQuery it was very easy, all I had to do what was add this line of code
$('.burger').click(function() { $('body').toggleClass('menu-open'); }); };
But I don't understand how I can achieve that in Angular with typescript! All the info I can find online pertains to toggling a class on the same element!
Just add a boolean property to your Component Class:
menuOpened: boolean = false;
Once the button is clicked, you need to do two things:
Toggle the menuOpened
:
<button (click)="menuOpened = !menuOpened">Click to Toggle Menu</button>
Add the class conditionally:
<div [class.menu-open]="menuOpened"></div>
Here's a Working Sample StackBlitz for your ref.
goes without angular simply: document.body.classList.add('menu-open');
to remove: document.body.classList.remove('menu-open')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.