如何在Typescript中的字符串中添加新行-Angular 8

如何在Typescript中的字符串中添加新行-Angular 8,angular,Angular,我试图在typescript中实现以下内容: this.comments = this.comment1 + '/n' + this.comment2 在HTML中,它被绑定为{{comments} 它应该打印: comment1 comment2 但它显示: comment1/ncomment2 我也试过,但不起作用。如何操作?您需要使用“\n”而不是“/n”您可以在任何模板元素中使用innerHTML和innerText指令。比如: TS @组件({ 选择器:“我的应用程序”, tem

我试图在typescript中实现以下内容:

this.comments = this.comment1 + '/n' + this.comment2
在HTML中,它被绑定为
{{comments}

它应该打印:

comment1
comment2
但它显示:

comment1/ncomment2

我也试过
,但不起作用。如何操作?

您需要使用“\n”而不是“/n”

您可以在任何模板元素中使用
innerHTML
innerText
指令。比如:

TS

@组件({
选择器:“我的应用程序”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent{
name='Hello
World'; 天气='Super\nsanny\nday'; }
HTML



演示供您参考:

您可以使用es6功能模板字符串进行尝试:-

this.comments = `${this.comment1}
${this.comment2}`;

当您使用typescript时,您应该使用es6功能来利用代码中的更多功能。

我已经尝试过了,这是否回答了您的问题?谢谢你的解决方案。我一整天都在为此而头痛。它工作得很好。Stackoverflow社区很棒:)
<div [innerHTML]="name"></div>
<div [innerText]="weather"></div>
this.comments = `${this.comment1}
${this.comment2}`;