0

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).

emka26
  • 433
  • 1
  • 11
  • 28

4 Answers4

0
$scope.displayString = _.keys(test).map(function(key) {
   return (key + '-' + test[key] + '\n')
})

or

$scope.displayString = _.keys(test).map(key => key + '-' + test[key] + '\n')
BaiLeiTao
  • 21
  • 6
0

You must use br/ in Html for line break.

0

You will need to add <br> instead of \n if displaying to html

$("#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>
Ashu
  • 2,066
  • 3
  • 19
  • 33
0

Use join("<br />") instead of join("\n") and also use html() function.

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>
Saeed
  • 5,413
  • 3
  • 26
  • 40
  • How can I avoid using jQuery? – emka26 Aug 08 '18 at 18:05
  • Are you using AngularJS? @emka26 – Saeed Aug 08 '18 at 18:08
  • Yes, and I want to pass $scope.displayString to html (for tooltip) – emka26 Aug 08 '18 at 18:10
  • Did you try `angular.element('#showHere').html(text)` instead? @emka26 – Saeed Aug 08 '18 at 18:11
  • But I need pass it for tooltip like this: . – emka26 Aug 08 '18 at 18:16
  • tooltip is a directive? Create a working example on codepen. @emka26 – Saeed Aug 08 '18 at 18:18
  • Yes, tooltip is a directive. Can't create codepen, because it's a part of huge project and it's many dependecies. But fact is that I must pass string of characters to directive in html and I want to have it with line break. – emka26 Aug 08 '18 at 18:29
  • Maybe using ` ` according to [this](https://stackoverflow.com/questions/3340802/add-line-break-within-tooltips) helps. I can not figure out how that directive works, so I can't help more. @emka26 – Saeed Aug 08 '18 at 18:32