javascript - Angular 管 : Cannot replace/n

标签 javascript angular ionic-framework ionic2

我一直在尝试制作一个管道来替换消息中的某些字符,这是管道的代码:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'replace'})
export class ReplacePipe implements PipeTransform {
  transform(value: string): string {
    console.log(value);
    let newValue = value.replace(/\n/g, '<br>');
    console.log(newValue);
    return `${newValue}`;
  }
}

我在页面上使用它是这样的:

  <ion-card-content [innerHtml]="message | linky | replace"></ion-card-content>

问题:当替换语句如下所示时它会起作用:

let newValue = value.replace('bit', '<br>');

但是当它看起来像这样时,它不会做任何事情:(我需要让它为此工作)

let newValue = value.replace(/\n/g, '<br>');

我似乎无法弄清楚我哪里出了问题。

最佳答案

我可以通过输入<br>来让它工作是 double quotes并添加额外的 \\n 。我不知道为什么我需要这样做才能使其正常工作,但现在它工作得很好。解决办法如下:

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'replace'})
export class ReplacePipe implements PipeTransform {
  transform(value: string): string {
    console.log(value);
    if(value) {
    let newValue = value.replace(/\\n/g, "<br>").replace(/&/g, "&amp;");
    console.log(newValue);
    return `${newValue}`;
  }
  }
}

关于javascript - Angular 管 : Cannot replace/n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392606/

相关文章:

javascript - 使用 Angular js 删除 IE8 中的#params

javascript - React Material Multi Select 默认值未选中

javascript - 显示空页面的侧面菜单 Ionic/angularjs

windows - ionic 2 启动失败,生成的命令出错

javascript - Ionic Android 应用程序启动后出现黑屏

javascript - Discord bot 客户端在尝试运行 bot 时丢失意图错误

javascript - 数据表自定义初始排序

angular - 在 Angular 中需要 BrowserAnimationsModule 但在 Universal 中出错

redirect - Angular 2 RC5 - 使用 Observable 重定向路由

javascript - Angular 5 : Get host name and app name from URL