code repo github.com/rick-chou/a…
Background: I hope to encapsulate my own message service but I don’t know how to use html in the service. The following is one of my solutions
Because I use NG-ZORRO The Notification component is used as the UI layer. [Related tutorial recommendations: "angularjs video tutorial"]
https://ng.ant.design/components/notification/en
NzNotificationService.template
The signature is as follows
template(template: TemplateRef, options?: NzNotificationDataOptions): NzNotificationRef;
So I need a customized TemplateRef to meet my needs
Idea 1
You can define methods in the service Pass it in from the business component, but there is no difference between using it directly in the business NzNotificationService.template
and there is no need for centralized processing
Idea 2
Give service Inject html template
Since you can't write html related code directly in the service, then follow the method of idea 1
Just call the initialization method in advance in a place that has nothing to do with the business
Using ng-template
will not generate a real dom node and the service is globally shared. With these two features, we can write the following code
message.service.ts
import { Injectable, TemplateRef } from '@angular/core'; import { NzNotificationService } from 'ng-zorro-antd/notification'; export enum EMessageCode { XXXError = 1024, YYYError = 1025, } export const MESSAGE = { [EMessageCode.XXXError]: 'XXXError...', [EMessageCode.YYYError]: 'YYYError...', }; @Injectable({ providedIn: 'root', }) export class MessageService { private templateMap = new Map<emessagecode>>(); constructor(private notificationService: NzNotificationService) {} // 初始化 templateRef public initTemplate(message: EMessageCode, ref: TemplateRef<any>): void { this.templateMap.set(message, ref); } public showMessage(messageCode: EMessageCode) { switch (messageCode) { case EMessageCode.XXXError: return this.notificationService.template(<templateref>>this.templateMap.get(messageCode), { nzDuration: 0, }); case EMessageCode.YYYError: { return this.notificationService.error('YYYError', MESSAGE[EMessageCode.YYYError]); } } } public removeMessage(messageId?: string) { this.notificationService.remove(messageId); } }</templateref></any></emessagecode>
message-service-virtual-ref.component
import { Component, TemplateRef, ViewChild, AfterViewInit } from '@angular/core'; import { EMessageCode, MessageService } from './message.service'; @Component({ selector: 'app-message-service-virtual-ref', template: ` <ng-template> <div> <span></span> <span> There are XXXError, you must refer to <a>something</a> to check out </span> </div> </ng-template> `, }) export class MessageServiceVirtualRefComponent implements AfterViewInit { @ViewChild('xxx_ref') xxxTemplateRef!: TemplateRef<any>; constructor(private messageService: MessageService) {} ngAfterViewInit(): void { this.messageService.initTemplate(EMessageCode.XXXError, this.xxxTemplateRef); } }</any>
app.component.html
<app-message-service-virtual-ref></app-message-service-virtual-ref> <router-outlet></router-outlet>
For more programming-related knowledge, please visit: Programming Video ! !
The above is the detailed content of How to use TemplateRef in Angular service. For more information, please follow other related articles on the PHP Chinese website!

本篇文章继续Angular的学习,带大家了解一下Angular中的元数据和装饰器,简单了解一下他们的用法,希望对大家有所帮助!

本篇文章带大家深入了解一下angular的状态管理器NgRx,介绍一下NgRx的使用方法,希望对大家有所帮助!

angular中怎么使用monaco-editor?下面本篇文章记录下最近的一次业务中用到的 monaco-editor 在 angular 中的使用,希望对大家有所帮助!

Angular项目过大,怎么合理拆分它?下面本篇文章给大家介绍一下合理拆分Angular项目的方法,希望对大家有所帮助!

本篇文章给大家分享一个Angular实战,了解一下angualr 结合 ng-zorro 如何快速开发一个后台系统,希望对大家有所帮助!

怎么自定义angular-datetime-picker格式?下面本篇文章聊聊自定义格式的方法,希望对大家有所帮助!

Angular Route中怎么提前获取数据?下面本篇文章给大家介绍一下从 Angular Route 中提前获取数据的方法,希望对大家有所帮助!

本篇文章带大家了解一下Angular中的独立组件,看看怎么在Angular中创建一个独立组件,怎么在独立组件中导入已有的模块,希望对大家有所帮助!


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version
