Skip to content

Opt out of Zone.js event patching in ngUpgrade #16491

@thelgevold

Description

@thelgevold

I'm submitting a ... (check one with "x")

[ ] bug report => search github for a similar issue or PR before submitting
[x] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior
Currently zone.js patches all native events like click, mousemove, keydown, etc to automatically trigger change detection cycles.

This is very convenient in pure Angular applications, but I have some concerns with this approach in ngUpgrade applications. Once patched, it appears that all handlers are enlisted in the Angular (4+) zone. Even the handlers that belong to Angular 1 code.

A side effect of this is that digest cycles are fired in the Angular 1 application every time one of the patched event handlers are invoked.

Example: If I have a mousemove handler registered in Angular 1, a digest is now triggered every time I move the mouse. This is similar to using ng-mousemove, but undesirable if you used mousemove in Angular 1, specifically to avoid digest cycles on every mouse move.

Expected behavior
The fix I have seen is to run the event handler for the Angular 1 event in the "parent" zone:

$window.Zone.current.parent.run(function() {
        $('body').on('mousemove', function(){
           console.log('triggered');
        });
});

I think it would be better if the parent zone was the default for Angular 1 events, instead of being available as an opt-in.

It makes sense that downgraded components have to run in the "change detection aware" zone, but this does not seem appropriate for existing Angular 1 handlers - at least not as the default.

Minimal reproduction of the problem with instructions
Register any native event handler in an ngUpgrade application

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 4.0.X
  • Browser: [all

  • Language: [TypeScript]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions