简体   繁体   中英

Detecting if a bootstrap navbar is collapsed or not in angular

I have a navbar inside a its own component called menu . I created a service, to be able to detect later on from other components if the navbar is collapsed or not. So anytime from other components I can refer to the service and determine if the navbar is collapsed.

The navbar is not collapsed if it has the class show .

So I can refer to the navbar in the menu component:

@ViewChild("navbar") navbar: ElementRef;

And I can check if it is collapsed or not using:

this.navbar.nativeElement.classList.contains("show")

How can I detect from inside the service if the navbar is collapsed?

Here is a running code:

https://stackblitz.com/edit/angular-pyquht

Created a Subject with Boolean value in service. Triggering next for each update of menu expand and collapse please go through the updated stackblitz

https://stackblitz.com/edit/angular-l7hw7p

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM