Repository navigation
Description
I'm submitting a
[X] feature request
I have a component which provides some generic business logic. Let's say:
- CRUD functionality
- Productmanagement (product catalogue)
- Cross department business management (separate layout for each department)
- ...
Depending on a condition I want to show a layout (template) which is specific for a type of product, related to the user department, etc.
Right now I can't load a template on the fly for doing this.
I have read through all the issues here and on stackoverflow for possible ways on doing this.
The most practicabel way is the ngTemplateLayout. But in real life if you have dozens of different layout types, it blows up your template file and make unmaintenable.
If you use the dynamiccomponentloader which is the most recommend way by the angular team, it adds an huge overhead of code for generating the component dynamically, even if the functionality for component and module creation is encapsulated. In addition it doesn't allow a real generic solution, because in the ngModule which has also be created dynamically you have to provide all imports, exports, providers, ... to make the component work. So if you don't want to have a huge overhead in your generic component builder, you have to implement a generic componentbuilder for each "type" of your form types. Which is not practicable and increases the danger of recoding all after the next angular release
Expected behavior
Provide a way of loading a template dynamically for a component. As it is available in Angular 1
Please don't be dogmatic on this issue and consider supporting real world requirements in Angular 2
Thanks
Activity
Toxicable commentedon Mar 18, 2017
Why not just have two components then load on whatever one corresponds to the situation that you need. For example:
AdminDisplayComponentandNotAdminDisplayComponent?.It's not possible to make templates dynamic, a template must be tied to a component
KarlXOL commentedon Mar 18, 2017
I know the template can't be dynamic right now and that's a pain as I outlined. I don't want to implement two, more or hundreds of components with the same logic.
That's why I request this feature!
Toxicable commentedon Mar 18, 2017
@KarlXOL What I mean by it not being possible is that it would be impossible to make it dynamic without breaking AOT.
As for a solution to your problem, if you want to share common code then why not use a service? or if you don't think a service will do make a component then inherit from it
KarlXOL commentedon Mar 18, 2017
@DzmitryShylovich: I know you don't want to provide this feature. But as I tried to explain all other ways for doing this, are crutches and not practicable.
If I do this by using angular features, I have also to use the compiler which also breaks AOT-capability. Right? So what.
Please, don't be dogmatic and rethink your approach. I/we are providing solutions and need a platform which is supporting us in doing this.
KarlXOL commentedon Mar 18, 2017
@Toxicable : If you have an example on how to encapsulate e.g. CRUD functionality in a service, I'd like to see. Even if this possible there is the need for implementing for each e.g. product a component.
mlc-mlapis commentedon Mar 18, 2017
@KarlXOL I do not understand why CRUD functionality could not be implemented as a service. Probably it is the best way how to do it and a common pattern.
If I understand correctly that you do not care about AOT at all and you want to do all in JIT mode with Compiler module loaded in runtime. Then you can do it right now. Look at for eaxmple at https://www.ag-grid.com/ag-grid-angular-aot-dynamic-components, https://eyalvardi.wordpress.com/2016/09/04/injecting-components-in-runtime or even http://blog.assaf.co/angular-2-harmony-aot-compilation-with-lazy-jit-2.
KarlXOL commentedon Mar 18, 2017
@mlc-mlapis : Of course the actual data operations (new, update, etc.) are in a service. But they get triggered by user interactions (click, ...) which are part of a component.
The link you are referring to, just shows how deal with multiple components. And that's the point, I don't want to have multiple components for red, green, blue. I just need one component and change the template/layout. In my feature request I've explained why
mlc-mlapis commentedon Mar 18, 2017
@KarlXOL But this example is exactly what you want, yes or no (to understand your point of view)? https://eyalvardi.wordpress.com/2016/09/04/injecting-components-in-runtime
Right, the clicks, ... are there but do not understand why you mention them because your components inject the service through DI (it should be a singleton) and will provide a necessary interface.
KarlXOL commentedon Mar 18, 2017
@mlc-mlapis This is one of the possible solutions I refer to in my request ( "dynamiccomponentloader"). The downside of this approach is
As a consequence you have a dynamic module with a huge overhead or you implement several dynamic module builders
In summary this solution is to heavy-weight, very low-level, risky that id needs to adapted as angular evolves,... and it is not competitive to the lean, quick approach Angular 1 provides for this requirement.
Just imagine you want to present a different "title" for users belonging to different departments to give just a imple example.
That's why I request the feature to change/load the template on the fly. The framework should deal with this low level stuff, not the business application.
mlc-mlapis commentedon Mar 18, 2017
It is clear now what you mean. I can imagine that type of simplification and having so high level of functionality. It is also necessary say that this would be strictly limited only for JIT mode.
Actually there is not any
attributein Angular which would differenciate its functionalities between JIT and AOT mode. There arestable,experimental,depreciated.Another question is the priority of such functionality in timeline and the standpoint of Angular core team.
KarlXOL commentedon Mar 18, 2017
@mlc-mlapis Thanks for your feedback. I'm happy I was able to bring the message across :-)
I know from many other issues here, stackoverflow, there is a big need for this feature as it is a very common requirement in real business applications. In the past and even more in the future.
KarlXOL commentedon Mar 18, 2017
@DzmitryShylovich I can't see from the ng-descendant proposal it will be a solution for my request.
I'd recommend you rethink the angular architectural approach to make it more flexible and usable for big enterprise application scenarios. It can't be an impossible mission for the angular framework and team to load templates dynamically for a component.
mlc-mlapis commentedon Mar 18, 2017
@KarlXOL Enterprise application scenarios are the reason why AOT exists. 😄 The dynamic templates are just shortcuts and a
cheapersolution. So probably the recommendation about rethinking that is unrealistic.KarlXOL commentedon Mar 18, 2017
@mlc-mlapis I expected an answer like this. The more I'm wondering why the angular team isn't providing what is needed by enterprises. And making applications faster and cheaper is key in enterprise environment. But you confirmed this is not what you are supporting with angular 2.
mlc-mlapis commentedon Mar 18, 2017
@KarlXOL Just out of curiosity, do you have any info about and how Google develops its global applications?
178 remaining items
BillyCottrell commentedon Oct 26, 2020
Hi, I have been searching for a way to load multiple templates into 1 single component for quite a while. Thanks to the post of @alarm9k I was able to find a way to do this. Ofcourse that way is only temporarily as it is not possible to launch it in production mode since it requires the compiler.
So for months I have been looking around for a way to do this but in production mode.
This actually didn't help since I couldn't find anything else so as a last resort I asked it on StackOverflow added some bounties to the question and add last I got an answer that is able to do this perfectly.
Ofcourse if you want separate stylesheets you can just add the stylesheet to the html template after you loaded it in from the database and it will work just fine (probably not the best but it works). Please don't credit me for this answer as all credits goes to the one who answered my question ofcourse.
https://stackoverflow.com/questions/63998467/angular-multiple-templates-in-one-component-based-on-id-with-template-store
SamuelMarks commentedon Oct 27, 2020
@BillyCottrell It also depends on what you know ahead-of-time. If you know all your templates ahead of time, you can always run a
nodescript (or similar) to interpolate them all together. There's probably ways of doing this directly withng, hacking around what little of webpack it exposes…BillyCottrell commentedon Oct 27, 2020
@SamuelMarks that is true, but this can be used when you request templates from a server since the innerHTML in the example will work async so you don't need to know the templates beforehand and even if you do you can still load them in beforehand making it only work faster since there is no need to request them from a server. It is even possible to combine it with a timer and switch template every so often. This is just an easy way to still load templates no matter what you know and you can refresh it afterwards if you want to so you can display a different template or perhaps a newer version of the template.
I mean you could easily create a templating service that has your pre-known templates. Add those templates to it and if you wanted you could either select one of the templates that are known or you add a new one from the server based on your needs.
The only thing one needs to do is to load in the service that has both the pre known templates as the self made templates from a server. Which is good if you can't load in a template from a server so you can fallback to one of the pre-known templates. So there is no need to hack around anymore.
SamuelMarks commentedon Oct 27, 2020
Yeah the premade templates is the key here. If you know AOT then you can have some fun, like with the aforementioned https://github.com/SamuelMarks/ng-md-components (that let's you use markdown templates). Trivial to upgrade its dependencies and add support for multiple templates to combine together (assuming anyone cares; just indicate).
hexoh commentedon Apr 7, 2021
@KarlXOL @Nixon-Joseph Funny, I had the same question. How do you solve this problem?
KarlXOL commentedon Apr 7, 2021
still unsolved (angular 11)
Nixon-Joseph commentedon Apr 7, 2021
@hexoh I don't expect the angular team is going to make this available. If this is something you're going to need to require, then you can either see the comments above (there's one with a github repo that has a good example of how to accomplish this (note, in my case I had to disable aot)) or maybe see if react can work for you. React is a library, not a framework so it can be much more flexible. It wouldn't surprise me to see a solution for this already available there.
If you want to get this working in angular, I suggest reading the whole thread.
artaommahe commentedon Apr 7, 2021
there is nothing speical in react to solve this. Same as with angular you have to dynamically create components recursively from some json (usually you can convert html string to json). With angular you also can use innerHTML + manual
ComponentFactory.create()over known DOM nodes (angular.io site used this approach before custom elements). Or disable aot and include compiler in runtime to automatically parce all angular stuff from the string;jessicajaniuk commentedon Jul 9, 2021
Thank you for submitting this feature request. The team has reviewed the feature request and has decided not to move forward with it. The approach described here would introduce significant XSS concerns and does not align with the angular architectural approach. We do see the need, though, and we will discuss addressing dynamically creating components in other creative approaches that should satisfy this need in a safe and robust way.
angular-automatic-lock-bot commentedon Aug 9, 2021
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.