Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert new line /n to a line break in angular

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;

like image 407
Rk R Bairi Avatar asked Dec 07 '17 21:12

Rk R Bairi


People also ask

Does \n make a new line?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

What is \n line break?

\r and \n are characters denoted with ASCII values of 13 (CR) and 10 (LF), respectively. They both represent a break between two lines, but operating systems use them differently. On Windows, a sequence of two characters is used to start a new line, CR immediately followed by LF.

How do you make a new line break?

Press ALT+ENTER to insert the line break.

How do you break a line in a string in typescript?

The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.


1 Answers

This does not replace it, but you can use the CSS attribute white-space: pre-line; to render the \n in the browser: https://developer.mozilla.org/en-US/docs/Web/CSS/white-space

div {
  white-space: pre-line;
}
<div>Foo
   Bar
       Baz     Foo
     Bar
 


     
  Baz
</div>
like image 147
Tim Schoch Avatar answered Sep 19 '22 15:09

Tim Schoch