单独文件中的angular2 ng-template [英] angular2 ng-template in a separate file
问题描述
angular2如何从其他文件使用ng-template?当我将ng-template放在可以使用的相同HTML内时,但是当我将ng-template移到单独的文件中时,它将无法正常工作.有没有办法将ng-template移到其自己的文件中并在其他html文件中使用它?
angular2 how to use ng-template from a different file? When I place the ng-template within the same HTML where I use it works but when I move ng-template into a separate file then it won't work. Is there a way to move ng-template into its own file and use it in different html file?
info-message.html
<ng-template #messageTemplate>
Hi
</ng-template>
<ng-container *ngTemplateOutlet="messageTemplate;"></ng-container>
上面的工作正常,因为ng-template和用法在同一文件中
above is working fine because ng-template and the usage is in same file
message-template.html
<ng-template #messageTemplate>
Hi
</ng-template>
info-message.html
<ng-container *ngTemplateOutlet="messageTemplate;"></ng-container>
这不起作用.有没有办法使用"messageTemplate" ,它位于另一个html(例如info-message.html)
This is not working. Is there a way to use "messageTemplate" which is in a separate file inside another html(Eg: info-message.html)
谢谢.
推荐答案
您看到了吗? https://github.com/angular/angular/issues/27503 dawidgarus提供的示例
Have you seen this? https://github.com/angular/angular/issues/27503 There is an example there provided by dawidgarus
建议是,如果要在不同文件中重复使用模板,则应将模板中的内容转换为单独的组件,然后可以在任何位置重复使用该组件.
The suggestion is that if you want to reuse your template in different files, you should convert what is inside the template into a separate component, then you can reuse that component wherever you want.
这篇关于单独文件中的angular2 ng-template的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!