Repository navigation
Description
I'm submitting a...
[ ] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report
[X ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior
I have HTML templates which is loaded from database. It has Form Controls, which I need dynamically render.
Currently in Angular 4.0 it is working fine, but we are not able to update to Angular 5.0 since the feature is not available
Expected behavior
I like to have the JIT Complier supported with AOT build. I am open to other option of displaying the controls.
Minimal reproduction of the problem with instructions
For bug reports please provide the STEPS TO REPRODUCE and if possible a MINIMAL DEMO of the problem via
This is working example with uses the compiler to build.
The top template has the First Name and Last Name.
This container is loaded dynamically with Controls Address, City, Radio options.
For this to work in AOT build I change the Complier to JIT Complier
What is the motivation / use case for changing the behavior?
All our use case are dynamically generated and it comes from Database. All validation are dynamically configured. Love to have the feature back.
Environment
Angular version: Working in Version 4.0.0, not working in Angular 5.0.0
Browser:
- [X ] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
For Tooling issues:
- Node version: XX
- Platform:
Others:
Activity
Toxicable commentedon Dec 8, 2017
Take a look at these
Angular Elements:
https://www.youtube.com/watch?v=ljsOPm4MMEo
Content Viewer
https://www.youtube.com/watch?v=__H65AsA_bE&feature=youtu.be&t=2h14m13s
Content Viewer Example
https://stackblitz.com/edit/angular-dynamic-content-viewer
vijayanandkannan commentedon Dec 8, 2017
@Toxicable Thanks for the Link.
I used the link to work on the way I need.
https://stackblitz.com/edit/angular-dynamic-content-viewer-pfmjl5
I tried to add they dynamic controls into the content. If you look the First Name is not dynamic, apart from it other controls are dynamic. I need to fire the validation and get the values. Also there are some controls which has dynamic validation fired.
The Plunkr link that I gave is working fine, but with JIT take out I am having difficulty.
Here is the link
http://plnkr.co/edit/rELaWPJ2cDJyCB55deTF?p=preview
I just need some help and directions, so I can work on some solutions without the need for JIT.
-Vijay
Torqode commentedon Dec 8, 2017
The Content Viewer doesn't have a dynamic template. All of the examples are showing how to create components (with static templates) on the fly, or how to dynamically change the pure html content inside a component.
What's being requested is the ability to create a component at runtime and provide it a template with Angular markup (e.g.,
{{ someValue | json }}, or<input [(ngModel)]="someValue" formControlName="someCtl">).Toxicable commentedon Dec 8, 2017
@vijayanandkannan Those examples I gave show how you can use static components that are known at build time along with dynamic layouts or content to avoid having to compile anything at runtime.
mlc-mlapis commentedon Dec 8, 2017
@Torqode ... you missed the basic principle of Angular concept ... there is no HTML template in run-time because you don't have Angular compiler in run-time so there is nothing what could compile your HTML code which contains Angular bindings.
Torqode commentedon Dec 8, 2017
@mlc-mlapis Yes, that much is understood. However, in Angular 4 we could include the JIT compiler in with an AOT build which would allow us to do this. But in Angular 5 (even though the JITCompilerFactory has been made public again and JIT compiling the application works), AOT compilation breaks. See #20639.
mlc-mlapis commentedon Dec 8, 2017
@Torqode ... I thought that the actual status is as Olivier said ...
#20639 (comment)
Is it true? ... because I didn't try that yet.
vijayanandkannan commentedon Dec 8, 2017
@Toxicable
I like to have the feature that was working in angular 4.0. We spent lot of time into this projects and planning to go live, when this was taken out in Angular 5.0.
What I meant is truly dynamic and we dont know what is coming from the server. All are configured in the server (Including the Valdiation). The plunk example that I gave is a sample of what is being done.
In angular 4.0 we Implement and create the compiler @ run time (This works perfectly with AOT Build)
import { JitCompilerFactory } from '@angular/compiler';
private compiler: Compiler = new JitCompilerFactory([{ useDebug: false, useJit: true }]).createCompiler();
mlc-mlapis commentedon Dec 8, 2017
@vijayanandkannan ... did you try what Olivier said and used just ngc with rollup, webpack, system js bundler or closure compiler? I can try that ... I believe that it works ... so you can have AOT + JIT together.
Torqode commentedon Dec 8, 2017
@mlc-mlapis I use webpack for both AOT and JIT builds, but I'm not sure how to compile just the module that needs the JIT compiler and then bundle it with the rest of the application. Is it possible to have the Angular CLI accept a single module to compile?
mlc-mlapis commentedon Dec 8, 2017
@Torqode ... I did a short test using just ngc to compile a sample to AOT app and then load a dynamic module in JIT mode in it. That module was loaded and compiled without any problem and I was able to create and place a new instance of its component. Now I am loading that module via
SystemJsNgModuleLoaderprovider from@angular/core.What I'll want to do is to use just a template string of any HTML with Angular bindings for that component ... just to simulate an operation of getting that HTML from CMS server for example.
mlc-mlapis commentedon Dec 8, 2017
@Torqode @vijayanandkannan ... I did also the second part with fully dynamic creation of
@Componentand@NgModule... just usingaddDynamicModulemethod inAppComponent... here no loader necessary andtemplateargument could be any HTML template:So everything is working ... AOT & JIT together. This is good info about Angular API. Another thing is using this with Angular CLI. Using directly Rollup, Webpack, SystemJS bundlers or Closure Compiler should be fine.
vijayanandkannan commentedon Dec 9, 2017
@mlc-mlapis Thanks for the Info. Are you using JITComplierFactory to create the complier or you are using the complier itself.
I am not to much familar with the Compliing part can you give me some steps of what you have done, so it will easy to work with
Example
Have you complied the entire module with ngc. Any optional parameters?
Also other steps that you have done.....
Thanks
Vijay
mlc-mlapis commentedon Dec 9, 2017
@vijayanandkannan ... I used the same as Olivier showed ... so providers:
and export function:
on the main app module level and compiler injected through DI:
and then using the function
addDynamicModuleas showed above on a component level. That's all. Compilation just viangc. I used directly the compiled files in AOT modeplatformBrowser().bootstrapModuleFactory(AppModuleNgFactory), so that's why I didn't have to optimize them ... tree-shaking and so on ... so I used"compilerOptions": {"target": "es5", "module": "commonjs", ...}.tsconfig.json41 remaining items
agrinko commentedon Feb 22, 2020
@MeeranRM , indeed, everything is much simpler now with Ivy.
This article helped me to successfully upgrade my plugin architecture to Angular 9: https://medium.com/angular-in-depth/asynchronous-modules-and-components-in-angular-ivy-1c1d79d45bd3
You don't need to override builder logic anymore to achieve dynamic plugins architecture: you only need custom builders in order to use webpack externals (to avoid bundling angular core and other libraries with the plugin code). I used ngx-build-plus with a custom webpack configfor this. Although I'm not sure it's necessary now. Maybe using ng-packagr would be sufficient for that.
You don't need any hooks to work with ng module factories - there are no module factories anymore! I just used that piece of code from the article to inject dynamically loaded external module to the main app:
Just instead of
importI used SystemJS v0.21.6 to be able to inject external dependencies for the webpack.In summary, JIT compilation is probably not needed anymore at all. Ivy uses AOT by default, and it enables truly dynamic architecture.
Lmk if you have more specific questions.
MeeranRM commentedon Feb 25, 2020
@agrinko thanks for ur reply. I understand your point. My requirement is to use the JIT compiler with the AOT build is to render the dynamic template at runtime.
My scenario:
I'm using ngx-dynamic module. please check this out for reference.
I used this below technique from that module to make the HTML string (which is fetched from the server) compiled dynamically. This code only worked with the help of the JIT compiler. It won't work in the AOT build.
So, could you please advise how to implement this scenario in angular ivy? or any other solution is appreciated
mlc-mlapis commentedon Feb 25, 2020
@MeeranRM The first thing is to be sure that your idea of dynamic compilation of external HTML is really necessary. In most of the cases, I saw before the main problem was that people were simply used to use this historical concept without re-thinking why and if they need it at all.
Generally, it's simply the way which is very problematic either with testing (you never know what comes as a template), performance and the final stability of such apps.
agrinko commentedon Feb 25, 2020
@MeeranRM not sure I understand your use-case. If you have some angular templates on the server and want to render them into your application, then it is the same as I did. But I do compile the components in a module using AOT to a single JS file and then inject it to the main app. I didn't even consider loading templates as pure strings because I need not only templates, but also styles and related TypeScript code.
snoop088 commentedon May 17, 2020
Hi @agrinko @MeeranRM
This is an excellent discussion. I am faced with a very similar questions.
But first, the use case - it is quite trivial I think - not sure why it has not come up or at least I cannot find any good solutions when using AOT post Angular 5. Imagine a simple CMS, where you can have a library of prebuilt components, but then you can extend them with hundreds of new templates that are built through some web UI and saved on the fly. The components can then be loaded on demand with selected template, should be able to leverage data from elsewhere in your angular application, reuse other components that you have exported, etc.
So when reading a ton of articles to find a good dynamic solution I bumped into this
import('./carousel/carousel.module').then(({ CarouselModule }) => {...but this is not truly dynamic? You do have a hardcoded static import string. What if you have 100 components from which you would like to build a dashboard?? How do you use the above import - surely not a switch-case? I think this type of static import is just a very specific use case, whereas the generic case would be that the component name is loaded from somwhere async.
Even if you are able to do some trickery like this:
which is far from great I admit.
Then you could try compiling on the fly to replace a template with one that you load from a db:
My idea is to reuse logic but be able to inject a template on the fly without the need to rebuild the whole app.
I tried this by injecting the JIT compiler, which in itself was a pain. It worked with aot=false (ng serve), it compiled with aot=true for production with optimisation, but then when running it it threw an error in the browser console..!?!
So @agrinko , I was wondering how did you manage to compile the components/ module with AOT and then inject it to the main app, which you mention above. I assume doing this did not need the app to be rebuilt? That could potentially be very useful in my use case even if it will require a certain server side step to invoke a built process when a user updates a template via the UI.. Is there any code example that is relevant to Ivy that you could point me to? I only found this on the web:
https://medium.com/angular-in-depth/building-extensible-dynamic-pluggable-enterprise-application-with-angular-aed8979faba5 but it uses custom webpack build scripts and I would much more prefer to build my components using angular-cli libraries.. Also the above seems to refer to Angular 8, not Ivy.
Sorry if my coding snippeds are not perfect, I am really not a good JS coder..
any pointers would be very very appreciated :)
thanks!
Nick
agrinko commentedon May 27, 2020
Hi @snoop088 .
Yes, you have exactly the same use-case as I do. Three Angular projects: Web, Mobile (with Ionic) and "web-templates" library, which includes such templates of the components. Whenever somebody changes/creates a new template, they only need to rebuild web-templates library, which is quick (and which can be handled automatically). The idea is to provide a UI for editing such templates later. And the new set of components is loaded to the main app via HTTP request after refreshing it. Although I don't load each component separately - I bundle them all in one module instead.
I even had an idea to write an article about it, though I'm too
lazybusy for that now.Here are the key aspects of my solution, and if you still need some help, let's contact, and I can explain more. Perhaps not the most elegant solution, but that's what we have with current Angular framework...
I. The instruction for the web-templates lib:
0. Solutions for Angular 7/8 and for Angular 9 are very different. Initially, I implemented for Angular 7, and it was very tricky. Now it's much simpler, although not without some tricks.
ng build --extra-webpack-config libs/web-templates/extra-webpack.config.js --project web-templates --single-bundlelibs/web-templates/extra-webpack.config.jscontains this:libs/web-templates/src/index.htmlis just a blank HTML document; it's there just because Angular requires it; but we don't need it.libs/web-templates/src/main.tsjust re-exports the main module that shall be consumed by the host application. The host application should know its name:libs/web-templates/src/lib/web-templates.module.tslooks like this:libs/web-templates/src/lib/template/template.component.html, which receives the inputs from the host application, and decides which exact component to render based on those inputs, using... yes, usingngSwitchCase😅 I belive there is a better solution if you have hundreds of components. You could organize them in separate modules, each with its own bundle JS, or export them from the main module. But for me it was fine. At least the host application doesn't care how the actual component is resolved.II. The instruction for the host application:
0. Install SystemJS as a dependency. For some reason, it only worked for me with this old version of SystemJS, not with any newer version:
"systemjs": "0.21.6"web-templates.servicewhich takes care of loading the external web-templates module, providing dependencies for it, etc. The file is too big, so you can see it here (ignore the rest, that sample isn't working): https://codesandbox.io/s/jolly-thunder-kqufn?file=/web-templates.service.tsthis.webTemplatesService.resolve().subscribe(...)and hope to get the external module to be injected into my appThis is just a rough explanation. If it really can be useful for somebody, I could prepare some better samples in the codesandbox.
P.S: Most of this implementation was inspired by this article: https://medium.com/angular-in-depth/asynchronous-modules-and-components-in-angular-ivy-1c1d79d45bd3
P.P.S: do you think I should provide this solution as a public library (at least for the web-templates.service part) and/or a sample repo?
snoop088 commentedon May 28, 2020
Hi @agrinko
thanks a lot for all the time taken to reply! It is a quite bit to read (seems there is no 4-5 line solution to this :))
I am starting on it and I will get back here with feedback once I get to the bottom of it. I have also read the above article that you mention few times. My problem is that whenever I try to import a compiled library, I always get 'Module not found' error in the console. However, I have used only import() as mentioned in the above article or "systemjs": "3.1.6". I have not tried with an older version and I have also compiled the library with the standard Angular 9 cli which is using ng-packagr because it was easier for testing, but is probably wrong. I will try ngx-build-plus:browser that you suggest next because of the externals handling. I tried importing the umd bundles from the built lib, but again I get 'Moduled not found'. I suspect that the issue is from the fact that import() and systemjs look into some path mappings to look for the module and do not load an arbitrary path which I am trying to load. My idea being to be able to put built libraries in some path without rebuilding the app.
I think a public github with a part of the project showing just the loading of the built library/ module would be great! To be honest I am still quite lost in this and I have resolved into importing the Angular Compiler at bootstrap as suggested here: https://www.angulararchitects.io/aktuelles/architecture-with-angular-ivy-part-2-higher-order-and-dynamic-components/ as it is by far the simplest solution I could find, but it causes bigger app bundle by around 300kb+ which is not negligible. Also it kind of does not go inline with the AOT idea as you compile on the fly and not ahead of time :))
anyway, thanks for your help, it is appreaciated. I will go over it in the next few days as I need to start making some decisions how to migrate our frontend from angular 5 using JIT to angular 9..10..n and AOT.
Nick
snoop088 commentedon Jun 11, 2020
Hi @agrinko
thanks again for your help here. I made it work :)
I am using nx.dev monorepo structure with buildable libraries. I have managed to implement a library that once built via ng-packgr for ivy can then be injected in the main app via SystemJS 0.21.6, which you correctly pointed out. Newer SystemJS may have switched to ES6 modules while this version works well with the umd bundles that ng-packgr builds. All is well so far and I can move on to the next stage of the software architecture design :). Hoping it will go smoother than this AOT injecting stuff hehe
again thanks for all your help. Happy to share more details if anyone is interested.
cheers,
Nick
pcatuna commentedon Dec 3, 2020
@snoop088 could you please share some details about your implementation ?
snoop088 commentedon Dec 9, 2020
Hi @pcatuna
I would be happy to share a little more detail. However, there is this thing that is currently pestering me in the approach that I have taken. It is using a library that is no longer maintained. "systemjs": "0.21.6" This is also what Alexey also mentioned above. It could be problematic if the next angular module import does not work with this library. This is why we didnt get this in production yet and we are still searching to possibly replace the systemjs with a later version that is being supported.
I prefer to spend some more time on this and possibly share something that would be a little more future proof instead.. Hopefully I find something before switching to react at the end :) It seems to be much more open with dynamic 'on-the-fly' component rendering than angular.
cheers!
Nick
hadrien-toma commentedon Dec 9, 2020
I take the opportunity to report than in Vue.js also it is much more open than in Angular for on the fly component creation/use. There even already exists a library for that: https://github.com/alexjoverm/v-runtime-template
Attempting support for AOT Builds using the logic from angular/angula…