Angular displaying multiple lined strings on one line

I am trying to get a string to be displayed on a webpage through Angularjs which may have multiple lines. Here is what I have got:

<textarea ng-model="StringValue"></textarea>
{{StringValue}}

When you type into the textarea:

“This is

a string”

you get:

“This is a string”

How do you get it so the text is displayed the same way it is typed?

Answer

As other answer mentioned, you can use a filter to achieve it.

Let me implement it for you:

<textarea ng-model="StringValue"></textarea>
<div ng-bind-html-unsafe="StringValue | breakFilter"></div>

angular.module('myApp', []).filter('breakFilter', function () {
    return function (text) {
        if (text !== undefined) return text.replace(/n/g, '<br />');
    };
});

Leave a Reply

Your email address will not be published. Required fields are marked *