Reputation: 1
I would like to dynamically determine the HTML template for Angular 2 component. Is that possible? How can we achive this functionality in Angular?
my objective is to have same code base/components but different HTML based some configuration/URL of the server? is that possible?
Upvotes: 0
Views: 1138
Reputation: 483
If possible I would suggest trying other methods to accomplish what you are trying to do. In my current project I did this originally by using compileModuleAndAllComponentsAsync to essentially build a module, a component, and compile and add that module at runtime to the project, and them manually inject it into the current component on screen using a ViewRef.
(More information on that can be found here)
However, this can really get out of control quickly.
This also prevented us from being able to use webpack because there was no way webpack was able to know what templates were required at compile time and relate them all to one component. This prevented us from using the Angular CLI which was bad.
Instead of all that I would suggest trying to find a way to do this with routing, and route to different components. This does mean you need a component for every possible template you have.
You might also look into the dynamic component loader since that is a supported feature of Angular 2.
Upvotes: 1