You need to define a <template> to be able to use it elsewhere in your app as a TemplateRef. You can store these TemplateRefs in a Service and then access them from any @Directive or @Component in your app.

We want to create a service and a component together to store all the templates.

service:

import {Injectable, TemplateRef} from "@angular/core";
@Injectable()
export class TemplatesService {
templates = new Map<string, TemplateRef<any>>();
}

compoment:

import {Component, ViewChild} from "@angular/core";
import {TemplatesService} from "./shared/services/templates.service";
@Component({
selector: 'template-storage',
template: `
<template #header>
<h1>This is header</h1>
</template>
<template #footer>
<h1>This is the footer</h1>
</template>
`
})
export class TemplateStorageComponent {
@ViewChild('header') headerTemplate;
@ViewChild('footer') footerTemplate;
constructor(private service: TemplatesService){ } ngAfterViewInit() {
this.service.templates.set('header', this.headerTemplate);
this.service.templates.set('footer', this.footerTemplate);
}
}

Here, set templates must to present in 'ngAfterViewInit'.

Directive:

import {Directive, TemplateRef, ViewContainerRef} from "@angular/core";
import {TemplatesService} from "../services/templates.service";
@Directive({
selector: '[surround]'
})
export class SurroundDirective {
constructor(
private template: TemplateRef<any>,
private view: ViewContainerRef,
private service: TemplatesService
){} ngAfterViewInit(){
this.view.createEmbeddedView(this.service.templates.get('header'));
this.view.createEmbeddedView(this.template);
this.view.createEmbeddedView(this.service.templates.get('footer'));
}
}

[Angular Directive] Create a Template Storage Service in Angular 2的更多相关文章

  1. [Windows Azure] How to use the Queue Storage Service

    How to use the Queue Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  2. [Windows Azure] How to use the Table Storage Service

    How to use the Table Storage Service version 1.7 version 2.0 This guide will show you how to perform ...

  3. [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET

    How to use the Windows Azure Blob Storage Service in .NET version 1.7 version 2.0 This guide will de ...

  4. angular directive scope

    angular directive scope 1.当directive 中不指定scope属性,则该directive 直接使用 app 的scope: 2.当directive 中指定scope属 ...

  5. [Angular 2] Share a Service Across Angular 2 Components and Modules

    Services are used to share data between components. They follow a module pattern that allows you to ...

  6. Windows Azure入门教学系列 (七):使用REST API访问Storage Service

    本文是Windows Azure入门教学的第七篇文章. 本文将会介绍如何使用REST API来直接访问Storage Service. 在前三篇教学中,我们已经学习了使用Windows Azure S ...

  7. [转]Work With Odata in Web API: Create Your First Odata Service

    本文转自:http://www.c-sharpcorner.com/UploadFile/dacca2/work-with-odata-in-web-api-create-your-first-oda ...

  8. 对象存储服务(Object Storage Service,简称 OSS)

    阿里云对象存储服务(Object Storage Service,简称 OSS),是阿里云提供的海量.安全.低成本.高可靠的云存储服务.它具有与平台无关的RESTful API接口,能够提供99.99 ...

  9. [AWS vs Azure] 云计算里AWS和Azure的探究(6) - Amazon Simple Storage Service 和 Microsoft Azure Blob Storage

    这几天Nasuni公司出了一份报告,分析了各个云厂商的云存储的性能,包括Amazon S3,Azure Blob Storage, Google Drive, HP以及Rackspace.其中性能上A ...

随机推荐

  1. 一句话解决Ping问题(主机,开发板,虚拟机)

    PC机使用网卡A连接开发板,VMWare就要使用同一个网卡A作为桥接网卡 步骤: 1.确定网卡A 2.VMWare选择网卡A作为桥接网卡 3.设置三者IP在同一网段 a.Windows网卡A的IP b ...

  2. 【Good Bye 2017 C】 New Year and Curling

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举前i-1个圆. 哪些圆和它相交. 取圆心纵坐标最大的那个圆就可以了. [代码] #include <bits/stdc++ ...

  3. 【hdu 3478】Catch

    [Link]:http://acm.hdu.edu.cn/showproblem.php?pid=3478 [Description] 一个人从起点s出发,假设他在时间t在节点x; 则在时间t+1,他 ...

  4. uva 1456(dp)

    题意:有n个数字u1,u2,u3-un,每一个数字出现的概率pi = ui/(u1 + u2 + - + un),分成w组.计算期望值. 第一组例子的五个数字例如以下 30 5 10 30 25 分成 ...

  5. 为什么golang的开发效率高(编译型的强类型语言、工程角度高、在开发上的高效率主要来自于后发优势,编译快、避免趁编译时间找产品妹妹搭讪,既是强类型语言又有gc,只要通过编译,非业务毛病就很少了)

    作者:阿猫链接:https://www.zhihu.com/question/21098952/answer/21813840来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出 ...

  6. Android实现微信分享及注意事项

    一.获取帮助文档并下载相关资料 首先打开微信开放平台:https://open.weixin.qq.com/ 如果没有注册,请先注册并上传开发者资料等待审核. 资源中心----移动应用开发----分享 ...

  7. Node.js笔记 请求方式 GET

    三种方法解析url 1. 传统的字符串split切割方法 2. querystring     只能解析数据部分,不能解析前面 index.html之类的地址部分. 3. url   可以解析地址和数 ...

  8. 00103_死锁、Lock接口、等待唤醒机制

    1.死锁 (1)同步锁使用的弊端:当线程任务中出现了多个同步(多个锁)时,如果同步中嵌套了其他的同步.这时容易引发一种现象:程序出现无限等待,这种现象我们称为死锁.这种情况能避免就避免掉: synch ...

  9. Cocos2d-x学习笔记(一)HelloWorld

    原创文章,转载请注明出处:http://blog.csdn.net/sfh366958228/article/details/38656755 前言 正式来公司实习已有一月,前一月主要是看了<C ...

  10. 为什么我要选择erlang+go进行server架构(2)

    原创文章,转载请注明出处:server非业余研究http://blog.csdn.net/erlib 作者Sunface 为什么我要选择Erlang呢? 一.erlang特别适合中小团队创业: erl ...