最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 dynamic template or ng-include - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question edited Sep 25, 2017 at 12:42 Sangwin Gawande 8,1668 gold badges52 silver badges67 bronze badges asked Sep 5, 2016 at 10:05 nCorenCore 2,0877 gold badges31 silver badges59 bronze badges 2
  • 1 There is ngTemplateOutlet but that doesn't sound exactly like what you are looking for. Another way is to compile components at runtime. – Günter Zöchbauer Commented Sep 5, 2016 at 10:06
  • you can try this solution : stackoverflow.com/questions/33749994/… – Anouar Mokhtari Commented Apr 10, 2017 at 8:44
Add a comment  | 

2 Answers 2

Reset to default 7

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).

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?

发布评论

评论列表(0)

  1. 暂无评论