Home convert new line /n to a line break in angular
Reply: 1

convert new line /n to a line break in angular

Rk R Bairi
Rk R Bairi Published in 2017-12-07 21:59:53Z

I have a string which contain new line character /n. Trying to display

the string. Instead of taking the /n as new line, it displays '/n' as text.

   $scope.myOutput = " Hello /n"

    {{ myOutput | textFormat }}

Required -> Hello (on html page)

Tried :

 app.filter('textFormat', function() {
    return function(x) {
      return x.replace(/\\n/g, '<br/>');

Tried css styles like white-space: pre;

Vadim Shashkov
Vadim Shashkov Reply to 2017-12-07 22:13:43Z

1 - rewrite your filter next way:

.filter('textFormat', function() {
    return function (x) {
      return x.replace(new RegExp('\/n', 'g'), '<br/>');

2 - in your html you should use the next syntax:

<span ng-bind-html="myOutput | textFormat"></span>

Where myOutput is $scope.myOutput = ' Hello /n'

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.302211 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO