1

I am displaying a message like this:

<td class="Msg">
  <div class="collapse">
    <h4 class="message">{{statement.Msg}}</h4>
  </div>
</td>

I have tried formatting in many ways but the message always appears like this:

a, b, c, d, e

But there should be a linebreak after each comma. When I look in FireBug, I see that the innerHTML is properly formatted.

How can I display the innerHTML of the statement.Msg?

EDIT: The message in the innerHTML is delimited by \r\n. That is how it's stored in the database and that's the innerHTML I see in FireBug, but it is not formatted that way on the page.

4
  • Like white-space: pre; on .message? Commented May 15, 2017 at 19:52
  • 1
    docs.angularjs.org/api/ng/service/$sce / <p ng-bind-html="myHTML"></p> Commented May 15, 2017 at 19:53
  • 1
    to clarify, the message in the innerHTML is delimited by \r\n. that is how its stored in the database and thats the innerHTML i see in FireBug, but it is not formatted that way on the page Commented May 15, 2017 at 19:55
  • you will have to run a msg.replace(/\r\n/, '<br />') if you want it to respect the line breaks. you can create filter break that does that for you and then you can bind the html to message|break. Commented May 15, 2017 at 20:01

2 Answers 2

3

I think you need to use ngSanitize and ng-bind-html:

<h4 ng-bind-html="statement.Msg"></h4>

And in your AngularJS module:

angular.module('myApp', ['ngSanitize']);

For more information (including installation information) please read: https://docs.angularjs.org/api/ngSanitize

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

2 Comments

I think that since the user wrote: "the message always appears like this: a, b, c, d, e" then it's just a css issue (As I wrote in my comment on the question), because I would expect they get something like a,<br>b,<br>c,<br>d,<br> using a regular expression, as demonstrated in the question
it's good to provide different options - it could be that your solution will do the job for him.
0

As I wrote in my comment: Add white-space: pre; on .message (Read here for all possible values available for the white-space style).

Example:

angular.module('app', []).controller('ctrl', function($scope) {
  $scope.statement = {Msg: "a,\r\nb,\r\nc,\r\nd"};
});
.message { white-space: pre; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="collapse" ng-app="app" ng-controller="ctrl">
    <h4>{{statement.Msg}}</h4>
    <h4 class="message">{{statement.Msg}}</h4>
</div>

Note that the first h4 element don't have message class so you won't get line breaks, while the second one do have this class.

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.