带换行符的 AngularJS 字符串,无中断显示 [英] AngularJS String with newlines, shown without breaks
本文介绍了带换行符的 AngularJS 字符串,无中断显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在数据库中,我正在保存来自文本区域的输入,您可以在其中添加中断.但是如何在 Angular 视图中显示它们呢?对于 PHP,这是 nl2br()
.
In the database I'm saving input from a textarea, where you can add breaks. But how to show them in a Angular view? For PHP this is nl2br()
.
像<div>{{ item.foobar }}</div>
,显示的中断像在数据库中一样保存.
Like <div>{{ item.foobar }}</div>
, with breaks shown that are saved like that in the database.
推荐答案
绑定 HTML 似乎有点不安全.CuriousGuy 建议的指令有一些额外的工程来正确转义 HTML.
Binding an HTML seems a bit unsafe. The directive suggested by CuriousGuy has a bit of extra engineering to correctly escape the HTML.
我发现使用样式规则 white-space: pre-line
更容易.
I find it easier to use a styling rule white-space: pre-line
.
JSFiddle 中的示例.
另见:
这篇关于带换行符的 AngularJS 字符串,无中断显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文