In order to integrate with an exising native JavaScript library that does DOM manipulation, I'd want to be able to attach an Angular-managed view to any DOM element in document
.
Let's say that SomeLibrary creates a container, somewhere in the DOM (we give it a target node where it can inject its HTML):
<body>
<angular-app-root>
<!-- here... -->
<div id="some-library-created-this"></div>
</angular-app-root>
<!-- or more likely, here... -->
<div id="some-library-created-this"></div>
</body>
I have a reference to the div#some-library-created-this
DOM node.
Now, I have an Angular component that can be used like this:
<angular-component [angularInput]="value">
<!-- what follows isn't the `template`, but what would get set in a ng-content inside of AngularComponent's template -->
<h1>Embedded Content Children</h1>
<hr>
<another-angular-component></another-angular-component>
</angular-component>
This AngularComponent
should now take the content that has been set inside of it, and display it inside the div#some-library-created-this
node we've got.
That is, the content of the component won't be displayed where the component has been declared, but somewhere else in any given native element.
Something like that:
<body>
<angular-app-root></angular-app-root>
<div id="some-library-created-this">
<some-kind-of-wrapper> <!-- ? -->
<h1>Embedded Content Children</h1>
<hr>
<another-angular-component></another-angular-component>
</some-kind-of-wrapper>
</div>
</body>
Is that even possible? Are there equivalent solutions that would let me benefit from SomeLibrary's unmanaged DOM manipulation logics while taking advantage of Angular's features?
I've read a few posts that show similar and advanced use cases, like these:
- https://medium.com/front-end-hacking/dynamically-add-components-to-the-dom-with-angular-71b0cb535286
- https://www.lucidchart.com/techblog/2017/04/10/using-angular-2-components-in-a-non-angular-app/ (very interesting, by the way)
But those only speak about creating components that are known in advance, and creating them specifically for a given view, not just take them and throw them elsewhere on the screen.