[英]Angular2 - getting a TemplateRef inside a service
Is there a way to get a TemplateRef for an existing html template inside a service? 有没有一种方法可以为服务中现有的html模板获取TemplateRef?
Context of the question (since I may be doing something wrong in the first place): 问题的上下文(因为一开始我可能做错了什么):
I'm creating a service which uses NgbModal to display some text in a popup. 我正在创建一个使用NgbModal在弹出窗口中显示一些文本的服务。 The modal requires a TemplateRef to display the contents, which I don't exactly know how to create.
模态要求使用TemplateRef来显示内容,我不知道该如何创建。 The other options are to pass a string (which is escaped, and I need to display html) or to pass a component (which I don't know how to pass the desired text to).
其他选项是传递字符串(已转义,并且我需要显示html)或传递组件(我不知道如何将所需的文本传递到)。
Two ways 两种方式
<template>
from the components template: <template>
: <template>
passed as child from the outside <template>
作为孩子从外面通过 (not tested) (未测试)
@Component({
selector: 'foo',
template: '<template #ref1>xxx</template>'
})
class MyComponent {
@ViewChild('ref1') template1:TemplateRef;
constructor(private template2:TemplateRef, private someService:SomeService) {
someService.template2 = template2;
}
ngAfterViewInit() {
this.someService.template1 = this.template1;
}
}
<foo><template>yyy</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.