首页 > 解决方案 > 从服务访问模板引用

问题描述

我想在服务中使用模板引用。例如:

// A component template that I want to use. Can be located anywhere.
<ng-template #announcement_tpl let-data="data">
  <div>{{data.title}}</div>
  <div>{{data.message}}</div>
</ng-template>


// Called from a callback in an injectable Service, outside the component class. 
this.notification_service.template(announcement_tpl)

我刚刚实施了这个解决方案。它有效,但我不确定这是否是一个好习惯。请注意,this.notification_service.template它不是从组件调用的,而是从服务的回调中调用的,因此不能简单地将模板传递给此方法。我认为理想的解决方案是导入组件并从组件中取出模板(以某种方式),除非当然有更好的方法。

标签: angularng-zorro-antd

解决方案


推荐阅读