我一直在尝试制作一个管道来替换消息中的某些字符,这是管道的代码:
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, "&");
console.log(newValue);
return `${newValue}`;
}
}
}
关于javascript - Angular 管 : Cannot replace/n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44392606/