我正在使用comments
在我的应用中构建Angularjs
功能。所以我在显示data
时遇到问题。我从这样的服务器获取数据:
This is the core collection of directives you would use in your template
code to build an AngularJS application.\n\nSome examples include: ngClick, ngInclude
数据包含\n
。它表示new Line
。为了显示该数据,我使用ng-bind-html
指令与$sce.trustAsHtml(data);
。但数据显示在同一行,而不是添加新行。
任何人都可以帮助我。
答案 0 :(得分:1)
默认情况下,HTML中会折叠空格。要解决这个问题,您应该使用<pre>
标记。空格不会在pre
标记内折叠。
<p>
<p>
lots of text
with new lines
look like this
</p>
很多文字 用新的线条 看起来像这样
<pre>
<pre>
lots of text
with new lines
look like this
</pre>
lots of text with new lines look like this
参考:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre
答案 1 :(得分:0)
您可以简单地将<pre>
替换为"\n"
,而不是使用假设您要格式化整个字符串的<br>
:
data.replace("\n","<br>");