1

I am Building comments feature in my application by using Angularjs. So I have a problem in displaying the data. I am getting data from server like this:

    This is the core collection of directives you would use in your template 
code to build an AngularJS application.\n\nSome examples include: ngClick, ngInclude

the data contains \n. it indicates new Line.For displaying that data I am using ng-bind-html directive with $sce.trustAsHtml(data);. But data is displaying in same line not added new lines.

Can any one Help me Please.

2 Answers 2

1

By default whitespace is collapsed within HTML. To get around that, you should use a <pre> tag. White space will no be collapsed within a pre tag.

with <p>

<p>
lots of text 
with new lines 
look like this
</p>

lots of text with new lines look like this

with <pre>

<pre>
lots of text 
with new lines 
look like this
</pre>
lots of text 
with new lines 
look like this

Reference: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of using <pre> which assumes you want to format the entire string, you can simply replace "\n" with <br>:

data.replace("\n","<br>");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.