Repository navigation
Description
Which @angular/* package(s) are relevant/releated to the feature request?
compiler, core, platform-browser-dynamic, platform-browser
Description
As far as I know the old aproach was to inject the JITCompiler and use it to render your HTML in an anonymous component.
This worked so far. I use editable HTML in DB which uses key features as substituting variables and directives like *ngIf or *ngSwitch.
Which does not work anymore in production mode (Angular 12.x)
I browsed stackoverflow and other forums to find an workarround for this and these are not working anymore (like loading the JITCompiler into Providers, which results in "JIT Compiler unavailable".
As I found not soulution for ivy/ng 12 i considered this feature as still missing.
Best,
Malte
Proposed solution
Component to render html strings.
Example:
<render-component [html]="..." [render-data]="{ show: true}></render-component>
[html] has the following value:
"<p *ngIf=\"show\">Hello!</p>"Which display in the case if show is true:
<p>Hello!</p>Alternatives considered
An Service to render html on the fly.
E.g.:
Activity
JoostK commentedon Jan 7, 2022
It should still be possible to compile templates at runtime using the JIT compiler, there may be something wrong with your config. Please be aware though that using the JIT compiler in production with dynamic inputs is a security concern.
nfMalde commentedon Jan 7, 2022
I also tried to rebuild the components one by one and fix linter errors / etc.
My configuration:
App Module:
My render component:
(Based on: HtmlCompiler)
My usage:
Result in dev mode:
Result in prod mode:
Which points directly to this function in angular->core.js:
I see no errors in my configuration so far.
JoostK commentedon Jan 7, 2022
Did you update using
ng update? In v12 some builder defaults were changed to support production builds by default, which means that things likeoptimizationnow defaults totrue(together with some more changes). Your configuration does not reflect these changes, and the stacktrace confirms that it's a production build. Production builds with full optimizations are not compatible with JIT mode, as important information has been dropped by the optimizer. You can rerun migrations usingYou shouldn't need any of the
nfMalde commentedon Jan 8, 2022
Thanks for pointing that out. Am I right with turning off the optimization the bundles are not minified anymore?
Main bug in my package.json was that somhow (I started the project with angular 5) the devkit builder was installed with version 0.5.x. Something must have beend wrong with my ng update calls...cant tell what but for now this is fixed.
I dont know if this is even a real version.
I turned off optimization and the rendering works, but as I pointed out the files are now full size and not minfied. Is there any other solution?
JoostK commentedon Jan 8, 2022
The CLI does not support optimizations together with JIT interop: #37216. It's likely possible to post-process the bundled files in order to just apply minification, e.g. using
esbuildorbabel.nfMalde commentedon Jan 10, 2022
Thanks! I now added terser-folder as npm script to minify it. I know that using dynamic rendering just because of route / click actions in the returned html of the server is not the best soulution and Im aware of the security risks of this. But so far I got no other idea since I have localized content from db like "To undo deletion click " which calls an function in the component.
Basically im only using this technique so far to use router links and click actions. Ill think of another solution in my next version but for now its working :-) Thanks!
angular-robot commentedon Jan 14, 2022
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list.
You can find more details about the feature request process in our documentation.
hydra1983 commentedon Jan 19, 2022
@nfMalde For v13, a workaround with minimum cost (bundle size) would be:
main.tsangular.jsonhydra1983 commentedon Feb 8, 2022
For angular v13 without injecting 'Compiler'
adhoc.type.tstsconfig.json22 remaining items
PH-Lars commentedon Mar 14, 2023
This does work indeed but for our project at the moment the bundle size, for example of main, goes from 300 kb to over 10MB. Good to know this is an option though, thanks for that.
For now Il wait if this ever becomes availible for build optimized.
PH-Lars commentedon Mar 15, 2023
Okay so I managed to find a way to make this work for now in our optimal scenario.
All buildoptimizers enabled etc, ivy compiling full.
So for me, I don't need this to work on every page or component. I made a stand-alone component which renders my html on pages where i need it.
I used the code above to create a anonymous module type, component type etc.
Then I added
import {ɵglobal} from '@angular/core';on top of the stand-alone component imports.
Then we gonne lazy load the compiler and publish the facade and after that we (lazy)loaded the jitcompiler to be used.
So one might, with this knowledge, create for example a service that just lazyloads the JIT compiler within the start up or in whatever way u want.
Now ur default app is Ivy with build optimized and u can get JIT without the initial bloat.
marvinerkes commentedon Apr 25, 2023
I am attempting to dynamically render content containing a component from an external library, but the tag remains empty. Does somebody have a clue on this?

jamend commentedon May 11, 2023
This seems to have the same issue as with loading
JitCompilerFactorywith aot off. The runtime module's module imports (ex.CommonModule) don't seem to work (#47160, #48034, angular/angular-cli#22570) songForetc. are not available.Strazz1337 commentedon Jul 9, 2023
I have no issues when using the standalone imports from
@angular/common.hiepxanh commentedon Aug 5, 2023
with this issue #51157
and https://github.com/MTobisch/ngx-dynamic-hooks
we can achive this in SSR Angular Universal
ref here too: Angular-Dynamic-Hooks/ngx-dynamic-hooks#34
ng-sp7 commentedon Oct 26, 2023
+1
ildarnabiev commentedon Dec 22, 2023
+1
NoelDeMartin commentedon Mar 4, 2024
We have been working on this in our app, and I want to share the workaround we've come up with.
TLDR, you need to apply the following patch to
@angular/compiler(we do it using patch-package):I realize how bad this looks. Patching the compiled code of
@angular/compiler, really o.O? Hopefully someone who knows more will let us know if we're doing something really dumb. But after spending some time working on this problem, this is the best solution I could come up with.I am not very familiar with the way Angular compilation works, so I had to do some source-diving and look at the compiled code in different scenarios. As far as I could gather, one of the things that AOT does is remove the imports from core modules (such as
CommonModule) and add them directly to components. That's why importingCommonModulein your dynamic components doesn't seem to work, but importing the individual directives or app modules does.For example, if you have
MyButtonComponentdeclared in a module that importsCommonModule, that's how you're able to use directives like*ngIfin its template. But using AOT, theimportsarray ofCommonModuleis left empty, and theNgIfdirective is added directly to thedependenciesarray ofMyButtonComponent.I assume this happens to improve runtime performance. When rendering component templates, only the directives that are actually used will be loaded. However, the actual code of the directives is still within the JavaScript bundle, it's not tree-shaken. That's why just by patching that line to include the imports, everything seems to work. You can see some of this logic documented in the R3SelectorScopeMode enum I'm using in the patch.
Knowing that, I'm aware that this workaround is not perfect, because it probably degrades de performance of non-dynamic components. But we've compared disabling AOT altogether vs this approach, and the performance difference is noticeable (this is better than disabling AOT).
Anyways, I just wanted to share what we've done in case it's useful to someone else. Or if someone who knows more about Angular internals can confirm or deny whether this is a good idea.
If you want to use this patch, do it at your own risk. But keep in mind that you may also need to add other external modules in your application. For example, we use Ionic and we included
IonicModulein thejitModulesarray of the patch as well.MOBILE-4527 compile: Fix JIT modules
vojtechmarek-dev commentedon Mar 27, 2024
Interesting. I have seen a lot of approaches but this is the first to tackle it on compiler level. I am not sure about patching angular's own complier and would be interested what others think about this approach.
We have been trying to get a hybrid AOT+JIT (dynamic rendering on demand) trying to work for quite some time, since our app depends on this feature.
We are in a process of upgrading from Angular 12 where such thing was possible, but alas on higher versions the dynamic rendering while having AOT enabled seems to be purpousefuly disabled and discouraged (according to this comment .
I can understand the reasoning behind this. However I still think there should be a way to have hybrid aot+jit app running.
alxhub commentedon Mar 29, 2024
@NoelDeMartin you shouldn't need to do any fancy compiler patching. For the interop to work,
ngJitModeneeds to be defined totruewhen bundling the application. This is the part that the CLI doesn't support today.NoelDeMartin commentedon Apr 2, 2024
Thanks @alxhub, can you please provide some example of how I can set
ngJitModetotrueif the CLI doesn't support it?I tried patching the following line to use
trueinstead and that doesn't seem to work:angular/packages/compiler-cli/private/tooling.ts
Line 31 in d74ee6e
It's not that I want to patch dependencies, of course. I'd much rather set some configuration options somewhere. Or even modify our application's code to support this. But I don't know much about the Angular codebase, and with my limited knowledge the only way I've managed to get it working is patching the compiler (I explained my rationale in #44660 (comment)). None of the other solutions in this thread seem to work.
MOBILE-4527 compile: Fix JIT modules
JeanMeche commentedon Jul 14, 2025
Because I was asked elsewhere for an example, here is one : https://stackblitz.com/edit/stackblitz-starters-2bgkkszp
This example is an AOT app that imports the compiler to allow JIT compilation for dynamically created components.
With the caveat that JIT compilation isn't recommended.