如何在角度2中替换字符串? [英] How to replace string in angular 2?
本文介绍了如何在角度2中替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在html页面中使用了以下插值。
I have used with below interpolation in html page.
<div>{{config.CompanyAddress.replace('\n','<br />')}}</div>
并且还使用
<div>{{config.CompanyAddress.toString().replace('\n','<br />')}}</div>
但两者都显示如下文字
{{config.CompanyAddress.replace('\n','<br />')}}
{{config.CompanyAddress.toString().replace('\n','<br />')}}
推荐答案
您可以使用相同的管道:
You can use a pipe for the same:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'replaceLineBreaks'})
export class ReplaceLineBreaks implements PipeTransform {
transform(value: string): string {
return value.replace(/\n/g, '<br/>');
}
}
管道必须包含在你的@NgModule声明中包含在应用程序中。
要在模板中显示HTML,您可以使用绑定outerHTML。
The pipe must be included in your @NgModule declarations to be included in the app. To show the HTML in your template you can use binding outerHTML.
<span [outerHTML]="config.CompanyAddress | replaceLineBreaks"></span>
这篇关于如何在角度2中替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文