4

I'm trying to concatenate a string with a "\n" like this:

- Item 1
- Item 2
var msg: string = '';
msg += '- Campo NOME é obrigatório';
msg += "\n- Campo EMAIL é obrigatório";

But the output comes without the line break:

- Item 1 - Item 2

I searched a lot about it but I didn't find a solution. Could you please help me? :)

2 Answers 2

4

If you rendering to HTML, you will want to use the HTML tag for a newline :

The string Hello\n\nTest in your source will look like this:

Hello!

Test

The string Hello<br><br>Test will look like this in HTML source:

Hello<br><br>Test

Try this :

var msg: string = "";
msg += "- Campo NOME é obrigatório";
msg += "<br/>- Campo EMAIL é obrigatório";

Here is fiddle : https://jsfiddle.net/9fuxgbms/1/

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

1 Comment

This does not work for me. My versions are - Angular CLI: 9.0.6 Node: 10.15.2
0

I am using Angular 15 and this works for me,

home.component.ts

headerText = "Hi, Enter Your \n Name";

home.component.html

<div class="header">
  <h1>{{headerText}}</h1>
</div>

home.component.scss

.header {
    margin-top: 100px;

    h1 {
      white-space: break-spaces; // <===== This is important
    }
  }

You can use white-space: break-spaces; or white-space: pre-line; depending on your usage.

Comments

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.