angular: Angular Element is not removed and readded to the DOM properly
š bug report
Affected Package
The issue is caused by package @angular/elements
Is this a regression?
No
Description
When an Angular Element is detached from the DOM and then re-attached, none of the event bindings work (see minimal reproduction for demo). I am a Google engineer and this is causing issues for us (internal bug filed here: http://b/168062117).
I talked to @kseamon about this and he thinks the issue is located in either of these 2 places:
Specifically, this.scheduledDestroyFn
in not set to null
in the scheduled destroy and so may be called again upon connect. Iām testing adding one line to see if it fixes the immediate issue.
However, even with this fix, there will still exist a tiny race condition that occurs when an element is scheduled to be destroyed and then connects before the destroy executes.
š¬ Minimal Reproduction
https://stackblitz.com/edit/angular-ovchwr?file=src%2Findex.html
š„ Exception or Error
No user-visible exception
š Your Environment
Angular Version:
Iām running it against the latest Angular in google3.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 4
- Comments: 24 (11 by maintainers)
Hi i have the same problem adding to angular js legacy app, ng-if breaks the angular web component.
To make sure we are on the same page, here is what the intention is with the current implementation:
This last bullet point is what we are failing to do at the moment, as mentioned in the previous comment (which is a bug), but that was the intention.
The reason we destroy the component instance is that there is some Angular-specific cleaning up that needs to happen when the element (and thus the underlying component instance) is no longer necessary (i.e. removed from the DOM for good).
The reason we have the short delay between removing the custom element from the DOM and destroying the component instance is to account for cases where you want to āmoveā the element around (i.e. remove it from its current location in the DOM and immediately insert it at a new location). In that case, it would be a waste to destroy the component synchronously, only to re-instantiate it right after.
From a quick look, it seems that fixing the current bug (i.e. allowing the component to be re-instantiated when the host element is re-inserted into the DOM) is not trivial. For example, how do we handle content projection. The original nodes have been extracted from the host element the first time, so they are no longer there for us to extract. (We could cache them and re-use the same nodes, but I wouldnāt be surprized if this broke in some edge cases.)
Even if we managed to fix this and allow the component to be re-instantiated, the internal state of the original component instance would not have been retained. This would lead to unexpected UI differences.
The main problem here is that there is no good way to automatically infer whether a removed element is intended to be re-used some time in the future (and thus the underlying component instance should be retained) or whether it is not needed any more (and thus the underlying component instance should be destroyed).
Therefore, we have to resort to unreliable heuristics (āif you havenāt added it back after 10ms you probably donāt need itā).
An alternative approach would be to provide a way for people to denote that they intend to re-use a particular custom element and therefore the underlying component instance should not be destroyed after the element has been removed from the DOM. Something like:
As a work-around, you can basically do this manually today (by using some private properties š):
https://github.com/angular/angular/issues/38778#issuecomment-697931683
I also bumped into this recently, we found that using a setter seems to give consistent results when trying to assign values from inputs on any subsequent initializations of an angular/elements web component.
The caveat is that the setters seem to run after the child component loaded in the
router-outlet
tries to access the value, which was solved with a boolean thatās set to true in the afterViewInit function.Something like this in the
AppComponent
:Doesnāt solve the root issue, but has unblocked our work thus far.
This bug is so severe that basically prevents Angular Elements from being used for developing a component library, unless developers are asked to avoid detaching and reattaching elements - which is a totally ludicrous request.
Iāve fiddled around this problem using a custom
NgElementStrategyFactory
class, basically copying the originalComponentNgElementStrategyFactory
(this is not good DX, folksā¦ I hope itāll get there) but removing thereturn
in theconnect
method (also references to Zone.js). That seemed fine and the component would get recreated, butā¦ then it crashes as soon asShadowDomRenderer
tries to reattach the Shadow DOM without checking if thereās one already (note: this is also incompatible with Declarative Shadow DOM) and eventually reusing or removing its content.I donāt know if itās worth considering making a PR out of this, since many other things are out of my comprehension of the problem, but let me know. I havenāt even checked if the other encapsulation modes are fine.
Anyway, Iād like to say that itās natural to compare the life cycle of Web Components to the one of Angular components, so that
ngOnInit
/ngAfterViewInit
are basically the equivalent ofconnectedCallback
, whilengOnDestroy
is basicallydisconnectedCallback
. As developers, I think we need to rely on this.The work-around at the bottom of https://github.com/angular/angular/issues/38778#issuecomment-690433315 does not work for us for 2 reasons: (1) the component is destroyed by the
@angular/animations
package (Iāve filed https://github.com/angular/angular/issues/38962 for that) and (2) the inputs are not reinitialized from the HTML attributes when it reconnects. Furthermore, we would prefer to not couple Angular Element removal logic with our host application.My main goal when filing this bug was to see if we could get removing and re-adding an Angular Element to work. I donāt mind if the element needs to re-render or reinitialize. I solely want the host application to be capable of removing and re-adding the element and the element to be in the correct state when re-added.
[Deleted comment as I was incorrectly thinking that there could be multiple elements assigned to a single strategy instance]
Disregard the part about the race condition - I think I was wrong about that. The comments below stand.