[英]Display string with line break
I need to display string with line break. 我需要显示带换行符的字符串。
I have object: 我有对象:
"test": {
"test1": 5,
"test2": 6
}
Now I need display something like this: 现在,我需要显示以下内容:
test1 - 5
test2 - 6
So I use: 所以我用:
$scope.displayString = _.keys(test).map(function(key) {
return (key + '-' + test[key])
}).join('\n')
But on view I have still string in one line, like: 但是在视图上我仍然将字符串放在一行中,例如:
test1 - 5 test2 - 6
It looks like I replace comma for one space, but I would like to have line break. 好像我用逗号代替了一个空格,但是我想换行。 How can I solve it?
我该如何解决? Thanks for any tip!
谢谢你的提示!
I don't want to use jQuery, I want pass $scope.displayString to my html (for tooltip). 我不想使用jQuery,我想将$ scope.displayString传递给我的html(用于工具提示)。
$scope.displayString = _.keys(test).map(function(key) {
return (key + '-' + test[key] + '\n')
})
or 要么
$scope.displayString = _.keys(test).map(key => key + '-' + test[key] + '\n')
您必须在HTML中使用br /来换行。
You will need to add <br>
instead of \\n if displaying to html 如果显示为html,则需要添加
<br>
而不是\\ n
$("#id1").html("test 1 "+"<br>"+" test 2"); $("#id2").html("test 1 "+"\\n"+" test 2");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="id1"></div> <hr> <div id="id2"></div>
Use join("<br />")
instead of join("\\n")
and also use html()
function. 使用
join("<br />")
代替join("\\n")
并使用html()
函数。
let object = { "test": { "test1": 5, "test2": 6 } }, text = Object.keys(object.test).map(function(key) { return (key + '-' + object.test[key]) }).join("<br />"); $('#showHere').html(text)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p id='showHere'> </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.