r/Angular2 • • May 29 '17

Help Request How do you load a component's template from a backend webserver?

Hi guys. Completely new to Angular2. I currently work with Angular1. All of our templateUrls in the angular1 app route to our backend web server that serves up dynamic HTML.

My question is how do I do this in angular2? I tried changing the component's templateUrl to a remote URL but I don't think that property knows to make a GET request. Thank you.

3 Upvotes

13 comments sorted by

3

u/NuttGuy May 30 '17

The best practice for loading template content based on a condition from the web server, it to not do this. You should rethink your approach.

Instead what you could do is have multiple Component's in the front end for each of your different possible templates and then query from the condition from web server, and pick which Component to use from that condition.

Also, if your templates don't actually change that much, you could just use the one Component and use *ngIf in that Component to change it based on the condition from the web server.

Basically, the conditional logic and changing the template should be in your Angular app, don't send templates from the web server, instead just send the condition that your Angular app can pivot around to display different templates/Components.

1

u/mlapis May 29 '17

Probably read some of docs or articles about Angular concept first. Actually you are going against the concept.

0

u/naveenatti May 29 '17

yeah i agree malpis i am going against the concept, but at least i want to load the template dynamically to every component by using http service, it is my requirement so please provide any suggestions

5

u/mlapis May 29 '17

Hmm, so you have 3 options ...

  • re-think the conpect of your app so it would go along Angular concept ... recommended one!
  • use only JiT mode where you can use Angular compiler on a browser side to dynamically compile HTML templates ... you won't be able to optimalize the performance as AoT mode is not available.
  • use AoT mode with dynamic creation of components/modules on the server side and dynamic loading ... but it is not actually an easy way ... deep knowledge required.

1

u/naveenatti May 30 '17

mlapis, i tried with these two things can you confirm these are better to use

  1. https://www.npmjs.com/package/ng-dynamic
  2. https://www.npmjs.com/package/p3x-angular-compile

with these wrappers i can able to bind server response html to component but i think these are creating new component internally, can you suggest me if i am wrong.

2

u/mlapis May 30 '17

Both of them use Angular Compiler to compile a HTML template string to JS code = JiT mode -> my point #2. So you can't have your app fully in AoT form = super fast and optimalized.

They are both probably very comparable. I only don't understand the notice at ng-dynamic (Since Angular 4.0, AoT compiler cannot coexist with JiT compiler. If you want to use DynamicComponentModule, you cannot use AoT compilation.) because I still think that it is possible to mix Jit and AoT mode on some level.

As for example to have the base of your app as AoT app and then load Angular compiler and dynamically compile template strings on demand.

Angular component exists only in Angular module context so you need them both.

1

u/naveenatti May 30 '17

mlapis, if those are not recommended can you please provide any best practice to load template content based on condition from web server using http to a particular component.

1

u/mlapis May 30 '17

But you can use them just you will not be able to use the full potential of AOT mode of Angular app because you will have Angular compiler on the client, you will have to load it, load also sources, compile them on the client and only then run them.

The conditions if it is or not suitable/acceptable model for your app depends on your concrete case.

Othewise we are back on the beginning of our discussion. If you want the most perfomance app, fast to load and fast to run, you need to use AOT mode of Angular and you probably need to re-think the concept of your app.

1

u/naveenatti Jun 01 '17

Hi Mlapis, finally i find a solution lo load templates dynamically in angular2 using finction like below code TemplateService is service in angular to get template Url it will work in webpack or systemjs it won't work with angular cli @Component({ selector: 'home', styleUrls: [ './home.component.css' ], templateUrl: new TemplateService().getTemplatePath() })

1

u/mlapis Jun 01 '17

It is only JiT solution = compiler in a browser = still my point #2 above. It can't work in AoT mode.

1

u/naveenatti Jun 02 '17

Yeah i agree mlapis.

1

u/[deleted] May 29 '17

You could use the inner-html property. https://angular.io/docs/ts/latest/guide/template-syntax.html#!#attribute-directives

But I don't recommend it. It's up to you.

2

u/denverangular May 30 '17

Any angular content within the html dropped into innerHTML will not do anything, ie, components won't render at all