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
@angular/elements + zone.js change detection related to RxJs stream runs in wrong zone #31870
Comments
@tomastrajan, I will check it. Thanks. |
@tomastrajan , this is really a very interesting issue, we may write a blog about it, I think I know the reason, but I am not sure I can explain it with text clearly, I will try to organize my wording first and talk to you soon. Also I will also try to find the solution, now I think share parent |
@JiaLiPassion thank you very much and looking forward to read the post! |
@tomastrajan. So here is the reason.
guide-repos.component.html
guide-repos.component.ts
So the trigger chain of the
Will need more discuss of this, so currently the easiest walk around is to share the Thanks. |
@JiaLiPassion thank you very much for such an in-depth answer! This makes a lot of sense and also corresponds with what we found when we logged out current zone inside of the stream operator. We will proceed with the Hope we will not encounter any other issues 😅 Anyway, thank you again and please, don't forget to add a link to a eventual blog post if that happens, wish you a great day! |
@JiaLiPassion I added description of the issue and possible solutions also here, maybe the project can be also of interest for you, it simplifies lazy loading of elements dramatically ;) |
@tomastrajan, thank you very much for creating the documentation, and I will try to find out an elegant solution for this issue. Have a nice day 😄 |
Hey @JiaLiPassion ! One more thing, the solution with reusing parent |
@tomastrajan , could you create a reproduce sample of |
Hey @JiaLiPassion , I debugged it further and it turned out to be something else, basically a stream was subscribed in The solution was to NOT subscribe in |
AFAICT, this issue has been fixed with #37814 (included in v10.1.0). I am going to close this. |
@gkalpak thank you, I will try it out! 👍 |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Reproduction
You can get running example of the last solution using this one liner
git clone https://github.com/tomastrajan/angular-elements-cd-example.git && cd angular-elements-cd-example && npm i && npm start
This will be working, it can be broken by removing{ ngZone: (window as any).ngZone }
from elementsmain.ts
file (as per description)Description
Basically it's about using
@angular/elements
inside of the Angular application. It all started with weird behavior in change detection but ONLY inrxjs
observable streams...The stream in element did not trigger change detection...
Example, we have an
@Input() username
and we display it inside of element but also use it to load repositories from github API, the change to username will:ngOnChanges
but the change will NOT:
Causes
It seems to be the case when current zone is logged out inside of the
rxjs
steam it is the parent Angular app zone instead of the element zone...Explored solution 1:
zone.js/dist/zone-patch-rxjs
I tried to solve it in various ways, the most promising was to add
import 'zone.js/dist/zone-patch-rxjs';
to the element BUT....This leads to second more serious problem ....
If the consumer SPA or ( any of it libs ) already uses
import 'zone.js/dist/zone-patch-rxjs';
then any subsequent call of import'zone.js/dist/zone-patch-rxjs';
which callsZone.__load_patch('rxjs', handler ...)
will be ignored because inzone.js
there isso then if element comes with it's own
rxjs
in its bundle and tries toimport 'zone.js/dist/zone-patch-rxjs';
patch it, it will be ignored because zone already hasrxjs
patch...Explored solution 2: pass parent Angualr app
NgZone
into the elementIn terms of solutions what we do now is
In element:
.bootstrapModule(AppModule, { ngZone: (window as any).ngZone })
In consumer SPA:
1.
The solution seems pretty dirty, would be curious about why we can't patch more than one
rxjs
and in what direction will the element go and if anybody ever struggled with this ?Reproduction (copy)
You can get running example of the last solution using this one liner
git clone https://github.com/tomastrajan/angular-elements-cd-example.git && cd angular-elements-cd-example && npm i && npm start
The error behavior (when rxjs does NOT trigger change detection after receiving of data) we can simply remove
{ ngZone: (window as any).ngZone }
from the elementmain.ts
fileQuestions
zone-patch-rxjs
for multiplerxjs
bundles in the single page ?NgZone
instance with the child element?cc: @JiaLiPassion @robwormald @manfredsteyer
Versions
Angular / CLI / elements 8
The text was updated successfully, but these errors were encountered: