Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 dynamic template or ng-include

Is there a way to load templates dynamically in angular2? In angular1 I used ng-include to load different html template in the main controller view. I know that angular2 can only take 1 templateUrl and been googling ng-include in angular2 and can't find any reference.

like image 211
nCore Avatar asked Sep 05 '16 10:09

nCore


2 Answers

Why do you need ng-include when you can make the html as a custom new component and use it wherever you need it, thanks to the selector tag.

Eg:

    @Component({
      selector: 'app-my-component',
      templateUrl: './my-component.component.html'
  })

So here, the my-component.component.html file can have the markup you want to include or show everywhere else. and the selector tag value <app-my-component></app-my-component> can be used throughout your application markup html files(you can think of it as your everyday html tags--but highly customized in nature and interpreted by the angular framework first aka custom tags but built using angular).

like image 154
siddharthrc Avatar answered Oct 20 '22 17:10

siddharthrc


Not sure about the dynamic template, but you can insert a dynamic component (which ofcourse will have a different template) .

A simple example, but deprecated can be found here : Angular2: Insert a dynamic component as child of a container in the DOM

An up to date example, but more complex can be found here : How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

like image 4
tibbus Avatar answered Oct 20 '22 19:10

tibbus