question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[9.0.0-rc.9] `CdkConnectedOverlay.backdropClick` events do not instigate change detection in `OnPush`

See original GitHub issue

Reproduction

See this repository as the StackBlitz template is not configured for v9 RC: https://github.com/benelliott/repro-cdk-overlay

Expected Behavior

What behavior were you expecting to see?

CdkConnectedOverlay.backdropClick events should be emitted within the Angular zone so that they are picked up by ChangeDetectionStrategy.OnPush.

Actual Behavior

Setting a value in a component based on the backdropClick event causes no changes under OnPush without a manual call to ChangeDetectorRef.detectChanges.

I think this is because in https://github.com/angular/components/blob/master/src/cdk/overlay/overlay-directives.ts#L380 the backdropClick.emit call needs to be run inside NgZone.run.

    if (this.hasBackdrop) {
      this._backdropSubscription = this._overlayRef.backdropClick().subscribe(event => {
        this.backdropClick.emit(event);
      });
    } else {
      this._backdropSubscription.unsubscribe();
    }

For some reason, this didn’t cause issues under Angular/CDK v8 but it causes issues in Ivy, presumably due to internal changes in Ivy’s change detector.

Environment

  • Angular: 9.0.0-rc.14
  • CDK/Material: 9.0.0-rc.9
  • Browser(s): Chrome 79
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows 10

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
visurelcommented, Jun 23, 2020

Issue still appearing with:

Angular: 9.1.9 CDK/Material: 9.2.4

1reaction
j2L4ecommented, Feb 26, 2020

This seems to be a more general problem. When you press the escape key, the overlay detaches, but the detach event doesn’t trigger change detection as well. Everything’s working fine, when you cdr.markForCheck() in the handler.

https://github.com/angular/components/blob/master/src/cdk/overlay/overlay-directives.ts#L287

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular OnPush Change Detection - Avoid Common Pitfalls
Scenario 1 - Our Starting Point (default change detection). Let's have a look at a simple component that does not use yet OnPush...
Read more >
UNPKG - @angular/material
270, * Event that emits whenever the raw value of the select changes. This is here primarily. 271, * to facilitate the two-way...
Read more >
@angular/cdk | Yarn - Package Manager
Designing and Implementing a CDK combobox component; Continuing to focus on improving the accessibility of Angular Material components. About the team. The ...
Read more >
how to prevent change detection when an event fired while ...
Your question is very vague, as you don't provide any code sample. For this reason it is really difficult to find a reason,...
Read more >
https://raw.githubusercontent.com/angular/material...
See the MDC migration guide for more information about the changes and how to ... do not run change detection if there are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found