0

I have a string str = "hello"+ "\n"+ "my"+ "\n" + "World".

html binding <div>{{str}}</div>

I expected to print this string as

Hello
my
world

I'm failing to do it but in console I'm getting what I want. Did I missing any thing here..

DEMO

4
  • 2
    <div style="white-space: pre">{{str}}</div> Commented Sep 10, 2018 at 11:46
  • How about rendering in pre tag? Commented Sep 10, 2018 at 11:51
  • yea looks good. Commented Sep 10, 2018 at 11:52
  • See this question. Commented Sep 10, 2018 at 12:12

1 Answer 1

1

Add <br> instead of \n and use innnerHtml tag in html

this.str = 'Hello' +'<br>'+ 'my' +'<br>' + 'world';

<div [innerHTML]="str"></div>

Demo

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

3 Comments

I'm using str for dynamic data, for each data I have different formats.
you can replace the tag with breaks like str.replace('\n', '<br>')
but for some reasons not preferring to use <br>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.