使用 CSS 显示带换行符的格式化 HTML 文本

tho*_*mas 2 html css angularjs

假设我有这样的文字:

$scope.text = 'This is the first line\nthis is the next line';
Run Code Online (Sandbox Code Playgroud)

我想在使用 angular 的 div 标签中显示它

<div>{{text}}</div>
Run Code Online (Sandbox Code Playgroud)

如何显示这个文本格式换行?现在它显示如下:

这是第一行这是下一行

Mik*_*ala 5

为了清晰起见,内联样式

<div style="white-space: pre;">{{ text }}</div>
Run Code Online (Sandbox Code Playgroud)

  • 还建议使用 `white-space: pre-wrap` 来保留空格和制表符,或者建议使用 `white-space: pre-line` 来折叠它们。 (2认同)