In header.component.html there is a button, by pressing which the menu in users.component.html should open. By clicking on the button a class is added to it. How can I add a class to a menu that is in another component (preferably without jQuery)?
header.component.ts
import {Component} from '@angular/core'; import {Router} from "@angular/router"; import {GlobalService} from "../../global.service"; @Component({ selector: 'header', providers: [GlobalService], templateUrl: 'app/_components/header/header.component.html' }) export class HeaderComponent{ public activeMobileMenuAdmin = false; public activeClass = false; constructor(public router: Router, public globalService: GlobalService){ router.events.subscribe((val) => { if (val.url === '/login' || val.url === '/users') { this.adminPanelView = false; } else { this.adminPanelView = true; } if (val.url === '/users'){ this.adminMenuView = true; this.detectDeviceWidth(); } else { this.adminMenuView = false; } }); this.activeClass = globalService.activeClass; } admMenuShow(){ this.activeClass = !this.activeClass; } ngOnInit() { this.detectDeviceWidth(); } detectDeviceWidth() { if (window.innerWidth < 1024) { this.activeMobileMenuAdmin = true; } else { this.activeMobileMenuAdmin = false; } } } header.component.html
<div class="menu-icon show-on-sm" [ngClass]="{'active': activeClass}" (click)="admMenuShow();" *ngIf="adminMenuView"> <span></span> <span></span> <span></span> users.component.ts
import {Component} from '@angular/core'; import {GlobalService} from "../../global.service"; @Component({ selector: 'admin', providers: [GlobalService], templateUrl: 'app/admin/users/users.component.html' }) export class AdminUsersComponent { private activeClass = true; constructor(public globalService: GlobalService){ this.activeClass = globalService.activeClass; } admMenuShow(){ this.activeClass = !this.activeClass; } } users.component.html
<div class="menu" id="admin-menu" [ngClass]="{'active': activeClass}"> <div class="contflex"> <div class="h1">Test</div> <ul> <li class="active">List 1</li> <li>List 2</li> <li>List 3</li> </ul> </div> global.service.ts
import {Injectable} from '@angular/core'; import {Router} from '@angular/router'; @Injectable() export class GlobalService { public user: Object = {}; public hideMenu: boolean = true; public activeClass: boolean = false; constructor(public _router: Router) {} admMenuShow(){ return this.activeClass = !this.activeClass; } onAuthError() { console.log('Works!'); } } The whole page has the following structure:
<header> ... <div class="menu-icon show-on-sm" [ngClass]="{'active': activeClass}" (click)="admMenuShow();" *ngIf="adminMenuView"> <span></span> <span></span> <span></span> </div> ... </header> <main> <router-outlet></router-outlet> <admin> ... <div class="menu" id="admin-menu" [ngClass]="{'active': activeClass}"> <div class="contflex"> <div class="h1">Menu</div> <ul> <li class="active">List 1</li> <li>List 2</li> <li>List 3</li> </ul> </div> </div> ... </admin> </main>