[Angular Directive] Create a Template Storage Service in Angular 2

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‘));
  }
}
时间: 2024-10-13 19:04:38

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

Angular Directive: link vs compile vs controller

Compile : This is the phase where Angular actually compiles your directive. This compile function is called just once for each references to the given directive. For example, say you are using the ng-repeat directive. ng-repeat will have to look up t

[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 use the data throughout your application so that your data is consistent and in sync. Create a service.module.ts: import { NgModule} from '@angular/co

angular directive和 webpack html-loader

问题: angular 的 directive支持使用template 和 templateUrl两种方式来指定模板 template的方式需要把html的内容放在js里,虽然有es6字符串可以换行, 少了编辑器对html的智能补全比较不爽 templateUrl的方式对于纯静态托管的angular项目比较友好 如果页面是后端服务器返回的话, 使用相对路径会找不到页面(这是当然的) 所以会用$templateCache 来把页面内容放到js里面(gulp 下例如ngtemplatecache 之

openstack安装(liberty)--安装块存储服务(Block Storage service/cinder)

八.安装块存储服务(Block Storage service/cinder) ###注意注意注意时间同步很重要 8.1安装环境准备中配置主机相应配置,包括主机名称,hosts,时间同步,防火墙,SELINUX以及相关OPENSTACK包 8.2控制节点配置 8.2.1创建数据库并授权 [[email protected] ~]# mysql -uroot -p Enter password: MariaDB [(none)]> create database cinder; Query OK,

angular directive restrict 的用法

E 表示该指令是一个element; A 表示该指令是attribute; C 表示该指令是class; M 表示该指令是注视 实例如下: 原帖:www.thinkster.io/angularjs/rep5re7gTM/angularjs-directive-restrictions While it’s cool to make a custom element like we did the the previous cast, it’s actually more common to d

满脑子都是Angular/directive/scope/git

坑1: directive要用到controller里面的东西呢,有两种办法: 通过$scope.xxx来设置的,直接用xxx引用 通过controller function 里面的 this.xxx 设置的,要通过controllerName.xxx引用 爬爬墙,更健康:附AngularJS Directive文档 坑2: git rebase和git merge有啥不同? 看看这:rebase的用法 满脑子都是Angular/directive/scope/git

ceph 译文 RADOS:A Scalable, Reliable Storage Service for Petabyte-scale Storage Clusters

RADOS:A Scalable, Reliable Storage Service for Petabyte-scale Storage Clusters 论文翻译 摘要 块式和面向对象的存储架构形成了一种以提升扩展性的存储cluster.然而,现存的系统继续把存储节点作为一个被动的设备,尽管他们有能力展示智能和自治.我们提出RADOS的设计和实现,RADOS是一个可靠的面向对象服务,通过利用每个独立节点的智能可以扩展到数千台设备.当允许节点半自治地通过利用集群地图来进行自我复制,错误检测,错

[Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor

If you're coming from AngularJS (v1.x) you probably remember the ng-true-value and ng-false-value directive which allowed to map custom boolean values like "yes" or "no" or whatever other value you had, onto your HTML form. In this les

S4B Storage Service Event 32054报错处理

Skype For Business S4B Storage Service had an EWS Autodiscovery failure Event ID 32054 If you get the Error with Event ID:32054 which has details like below Log Name: Lync ServerSource: LS Storage ServiceDate: 10/6/2016 8:37:34 AMEvent ID: 32054Task