angular / components

Component infrastructure and Material Design components for Angular
https://material.angular.io
MIT License
24.28k stars 6.72k forks source link

bug(material/sidenav): Unable to toggle sidenav from outside component #27359

Closed Marcel0024 closed 1 year ago

Marcel0024 commented 1 year ago

Is this a regression?

The previous version in which this bug was not present was

Below 14 i think (got solution from old examples)

Description

So toolbar in the header should open the sidenav further down. I have a header service, that has an event emitter. In the header component the button calls headerService.toggle.emit()

The way further down sibling subscribes to that event and should toggle the sidenav. I can see the event coming in but doesnt do anything.

@ViewChild('sidenav') sidenav!: MatSidenav;
this.headerService.toggle.subscribe(() => {
     this.sidenav.toggle();
});

I forked the 'Responsive sidenav' example from the sidenav component and made a repro

Reproduction

StackBlitz link: https://stackblitz.com/edit/t77lra?file=src%2Fexample%2Fsidenav-responsive-example.ts Steps to reproduce:

  1. Create service outside the component that has mat-sidnav
  2. Subscribing to that event and call sidenav.toggle() inside the subscription, should toggle the sidenav

Expected Behavior

Opening/toggle the sidenav from outside component

Actual Behavior

@ViewChild('sidenav') sidenav!: MatSidenav;
this.headerService.toggle.subscribe(() => {
     this.sidenav.toggle();
});

Does nothing

Environment

andrewseguin commented 1 year ago

This is because change detection is not running to check the sidenav state and run its animation. You can remove OnPush so that the click triggers zone's change detection, or manually run a change detection cycle

angular-automatic-lock-bot[bot] commented 1 year ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.