28

So I'm trying to do something very simple and I'm stuck. I have a String variable and within that variable I Wanna set line break so certain part of the text goes to new line.

What I have tried:

title: string = "My \n Title";
title: string = "My\ Title";
title: string = "My\
Title";
title: string = "My" + "\n" + "Title";

I have tried many variations but its just not working. Am I being stupid and missing something very obvious?

Not a duplicate as I have tried the <br/> and it has not worked.

Update:

The variable is being printed in the browser HTML like so {{title}}

17
  • 1
    Possible duplicate of New Line '\n' is not working in Typescript Commented Dec 6, 2017 at 15:43
  • 1
    @LucaKiebel not a duplicate as I have tried the solution in that question and it has not worked. Commented Dec 6, 2017 at 15:47
  • @Skywalker what exactly do you expect the newline to do? The \n escape definitely includes a newline character in a string. Depending on how the string is actually used, that may or may not be detectable. Commented Dec 6, 2017 at 15:48
  • 2
    Where exactly do you want to print it ? It matters: console.log or browser HTML render behaves differently. Commented Dec 6, 2017 at 15:52
  • 2
    @rweisse That's a linting preference, not a standard. Commented Dec 6, 2017 at 16:02

6 Answers 6

47

In html add style:

<div style="white-space: pre-line">{{DialogText}} </div>

Use '\n' to add newline in the typescript.

this.DialogText = "Hello" + '\n' + "World";

Same in stackblitz: https://stackblitz.com/edit/angular-rpoxr5linebreak

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

Comments

42

Here are two demonstrably working versions...

White Space

Solution One... if you want newlines to be respected in HTML... (works with the back-tick strings, or with 'My \ntitle'...

document.getElementById('example').innerHTML = `My
title`;
h1 {
  white-space: pre;
}
<h1 id="example">

</h1>

Angular Version:

<h1 style="white-space: pre;">{{title}}</h1>

HTML Break

Solution two... you want to use HTML...

document.getElementById('example').innerHTML = 'My<br />title';
<h1 id="example">

</h1>

Use ng-bind-html if you want to allow HTML during binding.

2 Comments

Close, but OP is using angularjs. Just make it <h1 style="white-space: pre;">{{title}}</h1> and this is a working solution for the OP
Hi @mhodges I wanted to create a short working example... but I'll add a note about that.
1

Pre-line / pre-wrap didn't work for me, unfortunately. Found out another solution that worked for me: For instance, your string variable keeps the next text

message = "Some text.\nSome text

Then in your html you can do:

@for(sentence of message.split("\\n"); track $index) {
    {{ sentence }} <br/>
}

In result you'll get all line breaks that your string has

Comments

0

You can also use a readonly textarea element instead of <div or <p> to keep the format of original string.

Comments

0

try like this

<div ng-bind-html="myMsg"></div>

$scope.myMsg =  `Below is the result:  <br>Successful:1,  <br>Failed:2`  // Use backtick

Comments

-2

You have done the right thing. But if you are showing this in a browser, the \n means didley.

You have to do:

title: string = "My<br>Title"

Now if you are using a fancy front end tool like React, you will have to deal with unsafe HTML in strings...

2 Comments

This question is tagged as "React", JSX will render "My<br>Title" with proposed code.
The accepted solution is excellent, because it's purely CSS and has nothing to do with React or Angular. JSX however will not render this string. You will have to do this: <div dangerouslySetInnerHTML={{__html: "My<br>Title"}}/>

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.