[Angular Directive] Create a Template Storage Service in Angular 2
2017-01-16 21:48
609 查看
You need to define a
We want to create a service and a component together to store all the templates.
service:
compoment:
Here, set templates must to present in 'ngAfterViewInit'.
Directive:
<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
@Directiveor
@Componentin 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')); } }
相关文章推荐
- Part 20 Create custom service in AngularJS
- [Angular] Create a custom validator for template driven forms in Angular
- Part 13 Create a custom filter in AngularJS
- c# - Create ServiceEndpoint in code and in config
- Part 18 $http service in AngularJS
- How to create simple web service in VS2010, NOT WCF service
- Create Dynamic Modal Dialog Form in Bootstrap template
- Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template
- [转]Work With Odata in Web API: Create Your First Odata Service
- [Angular Directive] Build a Directive that Tracks User Events in a Service in Angular 2
- [Angular] Using the Argon 2 Hashing Function In Our Sign Up Backend Service
- [Angular 2] Share Template Content In Another Template With Content Projection <ng-content>
- [Angular] Create a custom validator for reactive forms in Angular
- [ASP.NET AJAX]Important notes when we create web service methods for some extenders in AjaxControlToolkit package
- Create Script Template In Edit Mode
- Create a background service in Android
- [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword
- [Angular] Create a ng-true-value and ng-false-value in Angular by controlValueAccessor
- $q in Angular [ngdoc service]
- How to Create Custom Filters in AngularJs