[英]How to add new line in string in Typescript - Angular 8
我正在尝试在打字稿中实现以下内容:
this.comments = this.comment1 + '/n' + this.comment2
在 HTML 中,它被绑定为{{comments}}
。
它应该打印:
comment1
comment2
但它打印:
comment1/ncomment2
我也试过<br\\>
但它不起作用。 怎么做?
为此,您可以在任何模板元素中使用innerHTML
和innerText
指令。 喜欢:
TS
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Hello<br>World';
weather = 'Super\nsunny\nday';
}
HTML
<div [innerHTML]="name"></div>
<div [innerText]="weather"></div>
演示供您参考: https : //stackblitz.com/edit/angular-chusrl
您需要使用“\\n”而不是“/n”
您可以使用 es6 功能模板字符串尝试此操作:-
this.comments = `${this.comment1}
${this.comment2}`;
当您使用 typescript 时,您应该使用 es6 特性来利用代码中的更多功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.