Answer the question
In order to leave comments, you need to log in
Correct way to render HTML/CSS inside a component received from the server?
When developing an application on Angular 9, I ran into a problem with displaying HTML and CSS received from a third-party server.
To the point:
Inside the application there is a component with ad units.
Each ad unit consists of several parts that can change dynamically:
selectedTemplate: AdvertTemplate = null
selectedCharacter: AdvertCharacter = null
headline: string = ''
description: string = ''
<div class="tpl">
<h1 class="tpl__head">{{ title }}</h1>
<img class="tpl__img" src="{{ img }}">
</div>
.tpl {
display: flex;
}
.tpl__head {
font-size: 2rem;
}
.tpl__img {
display: block;
width: 10rem;
height: auto;
border-radius: 50rem;
}
I got all the templates, and when the user selected the desired template, using DomSanitizer, I inserted the HTML into the block I needed using [innerHTML] .
Issue: All variables inside {{ }} were displayed as text and the application could not interact with them. Didn't even get to CSS.
I tried to use Dynamic component loader
, but I couldn't figure out how rendering works. From different angles, various types of errors fell out into the console.
Answer the question
In order to leave comments, you need to log in
You need to use Jit Compiler, at runtime, (significantly increase the size of the bundle, it is desirable to take it to lazy)
Stackblitz example: https://stackblitz.com/github/lyczos/angular-dynam...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question