I have seen this SO question.
My code instead of ng-bind="item.desc"
uses {{item.desc}}
because I have a ng-repeat
before.
So my code:
<div ng-repeat="item in items">
{{item.description}}
</div>
The item description contains \n
for newlines which are not rendered.
How can the {{item.description}}
display the newlines easily assuming that I have the ng-repeat
above?
Try:
The
<pre>
wrapper will print text with\n
as textalso if you print the json, for better look use
json
filter, like:Demo
I agree with
@Paul Weber
thatwhite-space: pre-wrap;
is better approach, anyways using<pre>
- the quick way mostly for debug some stuff (if you don't want to waste time on styling)Based on @pilau s answer - but with an improvement that even the accepted answer does not have.
This will use newlines and whitespace as given, but also break content at the content boundaries. More information about the white-space property can be found here:
https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
If you want to break on newlines, but also collapse multiple spaces or white space preceeding the text (very similar to the original browser behaviour), you can use, as @aaki suggested:
Nice comparison of the different rendering modes: http://meyerweb.com/eric/css/tests/white-space.html
the css solution works, however you do not really get control on the styling. In my case I wanted a bit more space after the line break. Here is a directive I created to handle this (typescript):
Use:
All it does is wraps each part of the text in to a
<p>
tag. After that you can style it however you want.Just use the css style "white-space: pre-wrap" and you should be good to go. I've had the same issue where I need to handle error messages for which the line breaks and white spaces are really particular. I just added this inline where I was binding the data and it works like Charm!
I had a similar problem to you. I'm not that keen on the other answers here because they don't really allow you to style the newline behaviour very easily. I'm not sure if you have control over the original data, but the solution I adopted was to switch "items" from being an array of strings to being an array of arrays, where each item in the second array contained a line of text. That way you can do something like:
This way you can apply classes to the paragraphs and style them nicely with CSS.
Well it depends, if you want to bind datas, there shouldn't be any formatting in it, otherwise you can
bind-html
and dodescription.replace(/\\n/g, '<br>')
not sure it's what you want though.