J
J
Jeremy Jackson2015-01-23 07:12:45
Angular
Jeremy Jackson, 2015-01-23 07:12:45

How to escape all tags except br in angular?

The task is to make a line break in messages from clients.
There is a message from the client, which can contain anything, you need to replace line breaks (\n) with <br/>.
By default, angular cuts everything, and if you use $sce, then it displays everything as html.
And I basically need to escape the message first, then replace the line breaks with <br/>and display it as html.
I don't understand how it can be done

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Dedukhin, 2015-01-24
@jeremy13

.filter('nl2br', ['$sce', function ($sce) {
    return function (text) {
        return text ? $sce.trustAsHtml(text.replace('<', '&laquo;').replace('>', '&raquo;').replace(/\n/g, '<br/>')) : '';
    };
}]);

<div ng-bind-html="text | nl2br"></div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question