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. 【习题 7-2 UVA-225】Golygons

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 暴力枚举每次走哪里就好. 用一个二维数组来判重.(数据里,要求不能经过一个点两次->但路径可以相交 然后再用一个flag数组, ...

  2. libiconv 支持的编码

    libiconv 支持的编码 php 中的 iconv() 函数常用来作编码转换用.作一些不同编码的动态数据的转换时常遇到一些未知编码的数据,这时 iconv() 支持那些编码转换就很重要. 刚开始, ...

  3. mysql中配置ssl_key、ssl-cert、ssl-ca的路径及建立ssl连接(适用于5.7以下版本,5.7及以上请看本文末尾的备注)

    1.创建 CA 私钥和 CA 证书 (1)下载并安装openssl,将bin目录配置到环境变量: (2)设置openssl.cfg路径(若不设置会报错,找不到openssl配置文件) \bin\ope ...

  4. PHP中 “ . ” 和 “ ,”的区别

    在PHP中,“ . ”可以串接两个变量.而“ , ”却没什么用处.

  5. python基础--数值类型和序列类型

    Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1    #任意整数 float:2.3   #小数 python赋值: a = ...

  6. [python]类与对象-下

    [实例对象]可以简称为[实例] 一.类与对象的关系 [类]是[对象]的模板. [类]就像工厂的模具,以它为模板,造出来的成千上万的产品,才是被我们消费.购买.使用,真正融入我们生活的东西.这些产品,在 ...

  7. hibernate 的映射文件快速生成:使用CodeSmith快速生成映射文件和映射类

    一 CodeSmith简介 本文以表自动生成NHibernate的映射文件和映射类的实例来说明一下本软件的使用方法. CodeSmith是一种基于模板的代码生成工具,其使用类似于ASP.NET的语法来 ...

  8. 软件——关于C,typedef

    1;;写博客能让我慢下来,仔细思考 1;;这篇博客大多摘自网上 1;; 不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相 ...

  9. 关于PyYAML报错问题解决

    转自:http://www.fwqtg.net/%E5%85%B3%E4%BA%8Epyyaml%E6%8A%A5%E9%94%99%E9%97%AE%E9%A2%98%E8%A7%A3%E5%86% ...

  10. Loadrunner11--输入license后提示违反许可证安全,禁止操作

    安装中文补丁包后,重新把mlr5lprg.dll.lm70.dll覆盖LR11安装目录下“bin”文件夹下mlr5lprg.dll.lm70.dll.运行deletelicense.exe.重新用管理 ...