Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

314
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?

about 4 years 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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!