首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular 6-angular-通知程序

Angular 6-angular-通知程序
EN

Stack Overflow用户
提问于 2018-10-24 19:41:54
回答 2查看 3.8K关注 0票数 1

我正在使用angular-notifier进行通知。但我似乎找不到任何对自定义HTML的支持。当我将任何html标记附加到要在toast中显示的消息中时,html标记也呈现在消息中。如果有人有解决方案,请告诉我。

EN

回答 2

Stack Overflow用户

发布于 2018-10-26 02:07:02

可以使用ng-template自定义通知程序。首先在您的组件HTML中定义一个自定义ng-template:

代码语言:javascript
复制
<ng-template #customNotification let-notificationData="notification">
    <my-custom-alert type="notificationData.type">
        {{ notificationData.message }}
        // Here you can define any custom HTML
    </my-custom-alert>
</ng-template>

然后,在组件内部,您可以使用Angular的ViewChild使用模板变量#customNotification引用ng-template:

代码语言:javascript
复制
import { ViewChild } from '@angular/core'
...
@Component({...})
export class MyComponent {
@ViewChild('customNotification') customNotificationTmpl;
...
constructor(private notifierService: NotifierService) {}

showNotification() {
   const msg = {
       message: 'Hi there!',
       type: 'info'
   };

   this.notifier.show({
       message: msg.message,
       type: msg.type,
       template: this.customNotificationTmpl
    });
 }
}
票数 1
EN

Stack Overflow用户

发布于 2020-06-25 01:30:00

在documentation angular-notifier中有一种简单的方法

您可以在您的component.html文件中定义一个自定义ng模板,如下所示:

代码语言:javascript
复制
<notifier-container>
  <ng-template #customNotification let-notificationData="notification">
    <span type="notificationData.type">
      <svg width="24" height="24" viewBox="0 0 24 24">
        <path d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z" fill="#1976d2"/>
        <path d="M0 0h24v24H0z" fill="none"/>
      </svg>
      <span>{{ notificationData.message }} to angular-notifier</span>
    </span>
  </ng-template>
</notifier-container>

在您的组件中,您可以使用Angular的ViewChild通过其模板变量#customNotification引用:

代码语言:javascript
复制
import { ViewChild } from "@angular/core";
 
@Component({
    // ...
})
export class SomeComponent {
    @ViewChild("customNotification", { static: true }) customNotificationTmpl;
 
    constructor(private notifierService: NotifierService) {}
 
    showNotification() {
        this.notifier.show({
            message: "Hi there!",
            type: "info",
            template: this.customNotificationTmpl
        });
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52968101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档