2
<mat-drawer-content class="btmm md-sidenav-right">
  <button (click)="sideNav.toggle()" mat-button>Toggle SideNav</button>
</mat-drawer-content>

the above code is the button in one component

<mat-drawer #sideNav mode="push" opened="false" position="end" class="navsize _md-sidenav-backdrop">
  Drawer content
</mat-drawer>

this code want to be in another component

How can i do it please do it with my code and explain it

Aswin Kumar
  • 73
  • 2
  • 10

2 Answers2

4

To toggle a sidenav from another component you need:

  1. the property decorator @ViewChild.
  2. a service to accomplish this functionality.

for example let's say you have a header component that manages all the headers and several sidenav components one for each header.

so in the:

header

header.component.html

<mat-toolbar color="primary" class="example-toolbar" [ngSwitch]="headerType">
  <!-- HEADER_1 -->
  <mat-toolbar-row color="warn" class="example-toolbar" *ngSwitchCase="1">
    <button mat-icon-button (click)="toggleSidenav()">
      <mat-icon>menu</mat-icon>
    </button>
    <h1 class="example-app-name">HEADER 1</h1>
  </mat-toolbar-row>

  <!-- HEADER_2 -->
  <mat-toolbar-row color="warn" class="example-toolbar" *ngSwitchCase="2">
    <button mat-icon-button (click)="toggleSidenav()">
      <mat-icon>menu</mat-icon>
    </button>
    <h1 class="example-app-name">HEADER 2</h1>
  </mat-toolbar-row>

  <!-- DEFAULT HEADER -->
  <mat-toolbar-row *ngSwitchDefault>
   <h1 class="example-app-name">DEFAULT HEADER</h1>
  </mat-toolbar-row>
</mat-toolbar>

in the header.component.ts:

import { Component, OnInit, ChangeDetectorRef } from '@angular/core';
import { MediaMatcher } from '@angular/cdk/layout';
import { SidenavService } from '../services/sidenav.service';
@Component({
  selector: 'app-header',
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.scss']
})
export class HeaderComponent implements OnInit {
   headerType: number;
   toggleActive = false;

   mobileQuery: MediaQueryList;
   private _mobileQueryListener: () => void;

   constructor(private sidenav: SidenavService,
    changeDetectorRef: ChangeDetectorRef, media: MediaMatcher) {
    // to change the varible in the *ngSwitch directive
    this.newHeader(1);
    // this.newHeader(2);

    this.mobileQuery = media.matchMedia('(max-width: 600px)');
    this._mobileQueryListener = () => changeDetectorRef.detectChanges();
    this.mobileQuery.addListener(this._mobileQueryListener);
   }

   newHeader(headerType: number) {
     this.headerType = headerType;
   }

   toggleSidenav() {
        this.toggleActive = !this.toggleActive;
        this.sidenav.toggle();
   }
}

sidenav

in the sidenav.service.ts:

import { Injectable } from '@angular/core';
import { MatSidenav } from '@angular/material';

@Injectable({
  providedIn: 'root'
})
export class SidenavService {

  private sidenav: MatSidenav;

  constructor() { }

  public setSidenav(sidenav: MatSidenav) {
    this.sidenav = sidenav;
  }

  public open() {
    return this.sidenav.open();
  }


  public close() {
    return this.sidenav.close();
  }

  public toggle(): void {
    this.sidenav.toggle();
  }
}

let's say you have multiple sidenav components one for each header:

in sidenav1.component.html:

<div class="example-container" [class.example-is-mobile]="mobileQuery.matches">

  <app-header></app-header>

  <mat-sidenav-container class="example-sidenav-container" [style.marginTop.px]="mobileQuery.matches ? 56 : 0">
    <mat-sidenav #snav [mode]="mobileQuery.matches ? 'over' : 'side'" [fixedInViewport]="mobileQuery.matches"
      fixedTopGap="56">
      <mat-nav-list>
        <a mat-list-item routerLink="{{nav.link}}" *ngFor="let nav of fillerNav">{{nav.name}}</a>
      </mat-nav-list>
    </mat-sidenav>

    <mat-sidenav-content>
      <router-outlet></router-outlet>
    </mat-sidenav-content>
  </mat-sidenav-container>
</div>

in the sidenav1.component.ts:

import { MediaMatcher } from '@angular/cdk/layout';
import { Component, OnInit, OnDestroy, ChangeDetectorRef, ViewChild } from '@angular/core';
import { MatSidenav } from '@angular/material';
import { SidenavService } from 'src/app/services/sidenav.service';

@Component({
  selector: 'app-sidenav1',
  templateUrl: './sidenav1.component.html',
  styleUrls: ['./sidenav1.component.scss']
})
export class Sidenav1Component implements OnInit, OnDestroy {

  sidenavList1: Array<{ name: string, link: string }> = [
    { 'name': 'My profile', 'link': 'profile' },
    { 'name': 'Settings', 'link': 'settings' },
    { 'name': 'My messages', 'link': 'messages' },
    { 'name': 'Notifications', 'link': 'notifications' }];
  mobileQuery: MediaQueryList;
  fillerNav = Array.from(this.sidenavList1);
  fillerContent = Array.from({ length: 6 }, () =>
    ` --text content-- `);
  private _mobileQueryListener: () => void;

  @ViewChild('snav') public sidenav: MatSidenav;
  constructor(changeDetectorRef: ChangeDetectorRef, media: MediaMatcher, private sidenavService: SidenavService) {
    this.mobileQuery = media.matchMedia('(max-width: 600px)');
    this._mobileQueryListener = () => changeDetectorRef.detectChanges();
    this.mobileQuery.addListener(this._mobileQueryListener);
  }

  ngOnInit() {
    this.sidenavService.setSidenav(this.sidenav);
  }

  ngOnDestroy(): void {
    this.mobileQuery.removeListener(this._mobileQueryListener);
  }
}

for the other sidenav you reproduce the same procedure ...

AppComponent

in the app.component.ts:

export class AppComponent {
  title = 'my-app';
  headerType: number;
  constructor() {
    this.newHeader(1);
  }

  newHeader(headerType: string) {
    this.headerType = headerType;
  }
}

in the app.component.html

<div [ngSwitch]="headerType">
  <div *ngSwitchCase="1">
    <app-sidenav-1></app-sidenav-1>
  </div>
  <div *ngSwitchCase="2">
    <app-sidenav-2></app-sidenav-2>
  </div>
  <div *ngSwitchDefault>
    Default content
    <router-outlet></router-outlet>
  </div>
</div>
Dev M
  • 1,519
  • 3
  • 29
  • 50
1

If your components are not directly related (for example routes), maybe you can use a service.

Service to share the drawer instance

@Injectable({
    providedIn: 'root'
})
export class DrawerService {
    private matDrawer: MatDrawer;

    setDrawer(drawer: MatDrawer) {
        this.matDrawer = drawer;
    }

    toggle() {
         this.matDrawer.toggle();
    }
}

Component that cointains the mat-drawer

export class WithDrawerComponent implements OnInit {
  @ViewChild(MatDrawer) matDrawer: MatDrawer;
  constructor(private drawerService: DrawerService) {

  }

  ngOnInit() {
    this.drawerService.setDrawer(this.matDrawer);
  }
}

Component that activate toggle with a button

export class WithButtonComponent implements OnInit {
  constructor(private drawerService: DrawerService) {
  }

  toggleDrawer() {
    this.drawerService.toggle();
  }
}

I hope it helps you