angularangularjs-componentsangularjs-templates

Angular2 dynamically loading a template


I started doing some research into using Angular2 and one of the questions I had that I can't find a solution for. I have a requirement that depending on the data I receive from my model, I wish to load different templates for a given component. This is mainly related to maintaining different layouts depending on the data I receive. Is this possible?

Thanks


Solution

  • Angular 2 favors component composition approach to UI building. Because of this you shouldn't really end up with huge templates and if so it's maybe better idea to split them up into some helper sub-components (even if they provide nothing but layout).

    In cases when template isn't really big you can conditionally render based on some component's property with *ngIf directive.

    Another way to approach this would be to implement routes on that component level and then programatically route to these routes after you received the data with router.navigate(['./MySubcomponent'])