如何在Angular.js中的字符串插值中获取换行符 [英] How to get line break within string interpolation in Angularjs
问题描述
因此,我试图做一些非常简单的事情,但被卡住了.我有一个String
变量,我想在该变量中设置换行符,以便文本的某些部分移至新行.
So I'm trying to do something very simple and I'm stuck. I have a String
variable and within that variable I Wanna set line break so certain part of the text goes to new line.
我尝试过的事情:
title: string = "My \n Title";
title: string = "My\ Title";
title: string = "My\
Title";
title: string = "My" + "\n" + "Title";
我尝试了许多变体,但无法正常工作.我是不是很愚蠢,缺少明显的东西?
I have tried many variations but its just not working. Am I being stupid and missing something very obvious?
不是重复的,因为我尝试过<br/>
,但没有用.
Not a duplicate as I have tried the <br/>
and it has not worked.
更新:
像{{title}}
推荐答案
以下是两个可以正常工作的版本...
Here are two demonstrably working versions...
解决方案一...如果希望在HTML中使用换行符...(适用于反引号字符串或'My \ntitle'
...
Solution One... if you want newlines to be respected in HTML... (works with the back-tick strings, or with 'My \ntitle'
...
document.getElementById('example').innerHTML = `My
title`;
h1 {
white-space: pre;
}
<h1 id="example">
</h1>
Angular版本:
Angular Version:
<h1 style="white-space: pre;">{{title}}</h1>
HTML中断
解决方案二...您要使用HTML ...
HTML Break
Solution two... you want to use HTML...
document.getElementById('example').innerHTML = 'My<br />title';
<h1 id="example">
</h1>
如果要在绑定期间允许HTML,请使用ng-bind-html
这篇关于如何在Angular.js中的字符串插值中获取换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!