首页 > 解决方案 > Angular Notifier 中的新行

问题描述

由于我找不到这个决定,这里是 Angular 6 的一个简单问题。我正在使用angular 6服务angular-notofier,我试图为用户显示一个简单的多行消息。

我尝试过使用 HTML 标签<br/>和换行符"\n",但没有成功。看起来有字符串转义XSS或其他东西,我无法添加新行。

如果我可以那样做或者我试图以错误的方式做,有什么想法(如果是这样,那么为什么它是错的,我应该怎么做?)?

我想避免更复杂的结构,比如自定义模板(尽管我怀疑问题也应该出现在那里)。

这是我的“代码”:

constructor(
  private notifier: NotifierService,

...

this.notifier.notify(
  'error', 
  'Multiline' + "\n" + ' message will <br/> print in one line...' ....

非常感谢!安东

标签: angularnotificationsangular6newlinemultiline

解决方案


如果您想传递您的自定义消息,那么您可以利用该模板。通过模板,您可以完全控制消息的显示方式。

在 HTML 中

是的,您需要在 html 中使用模板来获得包含多行的自定义消息。

<ng-template #notification let-notificationData="notification">
       <span [innerHTML]="notificationData.message"></span>
</ng-template>

在 ts

 @ViewChild('notification') notificationTemplate;

 this.notifier.show({
           message: msg.message,
           type: msg.messageType,
           template: this.notificationTemplate //<-- template name from html.
        });

有关更多详细信息,请查看官方文档 - https://www.npmjs.com/package/angular-notifier


推荐阅读