如何在Angular service中使用TemplateRef
code repo github.com/rick-chou/a…
##背景:我希望封裝一個自己的message service 但是我不知道如何在service 中使用html 以下是我的一個解決方案
##因為我使用的NG-ZORRO的Notification 元件來做UI 層。 【相關教學推薦:《
NzNotificationService.template
簽章如下<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">template(template: TemplateRef, options?: NzNotificationDataOptions): NzNotificationRef;</pre><div class="contentsignin">登入後複製</div></div>
所以我需要自訂的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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

這篇文章帶大家繼續angular的學習,簡單了解一下Angular中的獨立組件(Standalone Component),希望對大家有幫助!

Angular.js是一種可自由存取的JavaScript平台,用於建立動態應用程式。它允許您透過擴展HTML的語法作為模板語言,以快速、清晰地表示應用程式的各個方面。 Angular.js提供了一系列工具,可協助您編寫、更新和測試程式碼。此外,它還提供了許多功能,如路由和表單管理。本指南將討論在Ubuntu24上安裝Angular的方法。首先,您需要安裝Node.js。 Node.js是一個基於ChromeV8引擎的JavaScript運行環境,可讓您在伺服器端執行JavaScript程式碼。要在Ub

這篇文章跟大家分享一個Angular實戰,了解一下angualr 結合 ng-zorro 如何快速開發一個後台系統,希望對大家有幫助!

angular中怎麼使用monaco-editor?以下這篇文章記錄下最近的一次業務中用到的 monaco-editor 在 angular 中的使用,希望對大家有幫助!

隨著網路的快速發展,前端開發技術也不斷改進與迭代。 PHP和Angular是兩種廣泛應用於前端開發的技術。 PHP是一種伺服器端腳本語言,可以處理表單、產生動態頁面和管理存取權限等任務。而Angular是一種JavaScript的框架,可以用來開發單一頁面應用程式和建構元件化的網頁應用程式。本篇文章將介紹如何使用PHP和Angular進行前端開發,以及如何將它們

這篇文章帶大家了解Angular中的獨立元件,看看怎麼在Angular中建立一個獨立元件,怎麼在獨立元件中導入已有的模組,希望對大家有幫助!
