code repo github.com/rick-chou/a…
##背景:我希望封裝一個自己的message service 但是我不知道如何在service 中使用html 以下是我的一個解決方案
##因為我使用的NG-ZORRO的Notification 元件來做UI 層。 【相關教學推薦:《
NzNotificationService.template
簽章如下<pre class="brush:php;toolbar:false">template(template: TemplateRef, options?: NzNotificationDataOptions): NzNotificationRef;</pre>
所以我需要自訂的TemplateRef 來滿足我的需求
思路一
#可以在service 中定義方法從業務元件傳入但是這樣和直接在業務中使用
NzNotificationService.template 沒有什麼區別也就沒有集中處理的必要了思路二
給service注入html template
既然不能直接在service 中書寫html 相關程式碼那就沿用思路一的方法
只不過事先在一處與業務無關的地方呼叫初始化的方法
利用
ng-template 不會產生真實的dom 節點以及service 是全域共享這兩個特性三我們就可以寫出如下程式碼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>
更多程式相關知識,請造訪:
程式設計影片以上是如何在Angular service中使用TemplateRef的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

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

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

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

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

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

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

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


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

Atom編輯器mac版下載
最受歡迎的的開源編輯器