Angular lets you access the native DOM of a component. You can use this to “embed” a Browser.element into an application.
What I’m struggling to wrap my head around is how this works with Angular’s change detection. Let’s say I embed my Elm element inside a component with a template like this
<div>
@if (something) {
<p> Display Something </p>
}
<div #elmElement></div>
</div>
Now, I assume change detection doesn’t actually work like this, but I’m essentially under the impression that the framework could redraw any part of the DOM it wants at any time. That is, if this is your Angular template, and something changes, there’s really nothing that says the whole component couldn’t be re-rendered in principal, unless Angular makes a promise somewhere that it won’t. Elm, for its part, points out that without the help of Html.Keyed, adding an element to a List can cause the diffing algorithm to update every subsequent node, which is fine since the view is a pure function of your Model, but Angular doesn’t know about your Model!
I guess what I’m getting at here is that embedding a native element that then gets modified outside of Angular feels like relying on some implementation detail inside Angular, hoping that your element persists, and I’ve tried to search for something that says otherwise, but I’ve come up short. They also say in the API docs for ElementRef that it’s a last resort (though I’m guessing that’s for security as they point out).
Use with caution
Use this API as the last resort when direct access to DOM is needed. Use templating and data-binding provided by Angular instead. If used, it is recommended in combination with
DomSanitizerfor maxiumum security;
Also, I don’t know what happens to a Browser.element if some external JS removes the DOM element that it’s “controlling” - does it crash, gracefully terminate itself, hang around in memory, updating an element tree that’s not actually in window.document anymore.
I’d appreciate it if anyone could answer any of these questions or link to some documentation that clearly outlines what guarantees Angular makes with respect to elements’ lifetimes.