Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start text from new line

Tags:

angular

I have two string. and I want to display the second string from new line. But I have to use only one variable in html.

let msg = "hello"+\n+"how r u";

I have to concat two string but start the second string from new line.

like image 905
rajeev tejapuriya Avatar asked Jun 21 '17 06:06

rajeev tejapuriya


People also ask

How do you start a new line of text in a cell?

To start a new line of text or add spacing between lines or paragraphs of text in a worksheet cell, press Alt+Enter to insert a line break. Double-click the cell in which you want to insert a line break (or select the cell and then press F2).

How do I move text from one line to another?

On all versions of Microsoft Excel for the PC and Windows, the keyboard shortcut Alt + Enter moves to the next line. To use this keyboard shortcut, type text in the cell and when ready for a new line, press and hold down the Alt key, then press the Enter key. The shortcut key can be used as many times as needed.

How do I move text to a new line in HTML?

To do a line break in HTML, use the <br> tag. Simply place the tag wherever you want to force a line break. Since an HTML line break is an empty element, there's no closing tag.

What is the \n in HTML?

<br>: The Line Break element. The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.


2 Answers

Import DomSanitizationService.

import {DomSanitizationService} from '@angular/platform-browser';

in constructor of class do this

constructor(sanitizer: DomSanitizationService) {
    this.msg = "Hello" + "<br/>" + "How are you?";
    this.msg = this.sanitizer.bypassSecurityTrustHtml(this.msg);
}

Use msg in the template as shown below

<p [innerHTML]="msg"></p>
like image 185
Mr_Perfect Avatar answered Nov 03 '22 00:11

Mr_Perfect


Since you have not described your problem completely, one possible and simple way is to use split function.

<p>{{msg.split('\n')[0]}}</p>
<p>{{msg.split('\n')[1]}}</p>
like image 23
SubbU Avatar answered Nov 03 '22 00:11

SubbU