Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

148
Views
Angular Material: How to close MatSidenav from inside a method in the component instead of from the html?

Using Angular + Angular Material 12

If you want to close the MatSidenav, then almost every solution I have found says to: (click)="sidenav.close()" in the html component.

But I need that (click) for my logout function (click)="onLogoutSideNav()"

onLogoutSideNav() {
    this.authService.logout();
  }

I need to close MatSidenav from inside a method in the component instead of from the html. The only solution I could find says to:

sidenav!: MatSidenav
...
onLogoutSideNav() {
    this.authService.logout();
    this.sidenav.close();
  }

But doing so returns undefined for this.sidenav.

There are a ton of solutions to use @ViewChild, but I haven't split my navs into header and sidebar components. I'm keeping it simple, doing so from within the app.component.

<mat-list-item *ngIf="isAuth" routerLink="/"><button mat-icon-button><mat-icon>logout</mat-icon><span class="sidenav-span" (click)="onLogoutSideNav()">Logout</span></button></mat-list-item>

What am I missing here?

7 months ago · Juan Pablo Isaza
1 answers
Answer question

0

you can call multiple functions on click event. eg =>

(click)="onLogoutSideNav();test()"

hope this answers your question.

7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs