#How to (properly) render standalone components dynamically?

26 messages · Page 1 of 1 (latest)

rugged stag
#

I'm trying to render a standalone component into another non-standalone component. The problem is: the styles of the standalone component isn't being applied while rendering it dynamically, but it works fine if I do it in the HTML template. And it turns out that the usual rendering puts some random _ng* attributes on the rendered elements, which are missing when I do it dynamically. In my reproduction at https://stackblitz.com/edit/angular-ivy-gcjhpa, the component isn't even rendered. Now my question is: how to do it properly?

#

Take a look at this image below. The upper portion of the drawn line is rendered dynamically, while the bottom portion is rendered by adding the component's selector directly in the template.

woeful bison
#

g!badeyes @rugged stag Hard to read and copy and paste text from an image.

pure magnetBOT
#

@rugged stag Please do not post pictures of code (especially photos of a physical screen) as they are difficult to read and difficult to correct (as you cannot copy code from a screenshot to modify it).

woeful bison
#

What does "rendering it dynamically" mean?

rugged stag
outer oar
#

In your repro you're not adding dynamically the component.
You're just appending a DOM node named test

#

To make what you want you should probably use ViewContainerRef

woeful bison
rugged stag
outer oar
#

Create a DOM node with tagname as specified as argument

rugged stag
outer oar
#

Template gets parsed and relative components get generated.

rugged stag
woeful bison
#

Why do you want to do it this way? It is rather advanced and rare Angular

rugged stag
rugged stag
woeful bison
#

You poor poor soul. I do that. I wouldn't if I wasn't already.
I turn off AOT and bloat my app with the Compiler at runtime.
It is terrible.

#

Have you considered making Vue Custom Elements instead of doing something that Angular actively fights against you with?

outer oar
woeful bison
#

I consider Vue because the custom compiled components in my app don't really have to interact with the rest of the app directly. They can just go through Firebase Firestore

woeful bison
#

React custom elements? Almost any other SPA has (last I checked) the ability to compile a template from the server into a component in the browser.

#

oh, and note that solutions that work in StackBlitz or in ng serve do not necessarily work with ng build

ruby bay
#

Custom Elements are a pretty good solution here - that's what angular.io does