Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Output not working properly from global context even with ngzone.run() #46288

Closed
KostyaTretyak opened this issue Jun 7, 2022 · 3 comments
Closed

Comments

@KostyaTretyak
Copy link
Contributor

KostyaTretyak commented Jun 7, 2022

Which @angular/* package(s) are the source of the bug?

core

Description

If for @Output you create a callback in a global context (for example, as a property of a window object) and then try to emits events from this callback, only the first call will work. Further attempts to make emits will not work even with ngzone.run().

You can see this bug on stackblitz or on my repository. If you try to click the "first" link and then the "second" link, everything will work as expected. But if you try to do it again, when you click on "second" you will not see the output message from the child mesage from child @Output.

In my case, I created several child components, each of which works with its vendor to login through social networks (Facebook, Google, etc.). These vendors call callbacks from a global context. In this case, I can't rely on using @Output because they don't work properly.

Environment

Angular CLI: 13.3.7
Node: 16.15.0
Package Manager: yarn 1.22.1
OS: linux x64

Angular: 13.3.11
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1303.7
@angular-devkit/build-angular   13.3.7
@angular-devkit/core            13.3.7
@angular-devkit/schematics      13.3.7
@angular/cli                    13.3.7
@schematics/angular             13.3.7
rxjs                            7.5.5
typescript                      4.6.4
@JoostK
Copy link
Member

JoostK commented Jun 7, 2022

You are assigning testCallback here which closes over this, i.e. the first component instance of app-child:

    (window as any).testCallback = () => {
      this.zone.run(() => {
        this.someEvent.emit('mesage from child @Output');
      });
    };

New instances of app-child don't rebind that closure, so the closure state still refers to the original component that has since been destroyed. Hence, emitting into its output has no effect.

@JoostK JoostK closed this as completed Jun 7, 2022
@KostyaTretyak
Copy link
Contributor Author

I felt that would be the case. It's a shame, but relief still came. Thank you!

@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants