-2
    let samplestring ="a:b;c:d;e:f"
    this.detail = samplestring.split(";").join("\n");
<div fxLayout="row">
    <span class="default_label_font">{{detail}}</span>
  </div>

but not adding new line and giving response like response sample i'm getting

I've tried using "
" too but instead of adding break it concatenate it

I refer Split string with commas to new line

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
Himanshi
  • 84
  • 1
  • 2
  • 13

4 Answers4

1

Another way to do it, I think much simpler, is to use [innerHtml] (docs) instead of interpolation and break lines with <br>.

Like this:

<div fxLayout="row">
  <span class="default_label_font" [innerHtml]="detail"></span>
</div>
let samplestring = "a:b;c:d;e:f";
this.detail = samplestring.split(";").join("<br>");

StackBlitz for you to test and play.

vicpermir
  • 3,544
  • 3
  • 22
  • 34
0

If you need to print the text on separate lines you will have to add <br> inbetween. \n is not rendered by HTML, unless you use <pre>.

extract from component.ts

this.lines = samplestring.split(";");

extract from component.html

<ng-container *ngFor="let line of lines; i = index">
  <br *ngIf="i != 0">{{line}}
</ng-container>
Akxe
  • 9,694
  • 3
  • 36
  • 71
0

Issue is the element you are using to set. Span is a inline component. So line breaks will not work. You will have to use a block level element like Div

let samplestring = "a:b;c:d;e:f"
const detail = samplestring.split(";").join("\n");

document.querySelector('.default_label_font').innerText = detail;
<div fxLayout="row">
  <div class="default_label_font"></div>
</div>
Rajesh
  • 24,354
  • 5
  • 48
  • 79
  • First of all, its not jQuery. Second, this is to depict the use of correct element. I can help you solve the issue not your code – Rajesh Mar 19 '20 at 09:34
-1

HTML

<h1 id="ovde">
      ...
    </h1>

ts

let customItems = "1,2,3";
  customItems = customItems.split(',');

  for ( let i = 0; i < customItems.length; i++ )
    customItems[i] = "- " + customItems[i] + "\n";

  customItems = customItems.join('');

  document.getElementById("ovde").innerHTML = customItems;
Bansi29
  • 1,053
  • 6
  • 24
Harsh Lad
  • 9
  • 3
  • angular way please – Himanshi Mar 19 '20 at 09:16
  • const elements = ['Fire', 'Air', 'Water']; console.log(elements.join()); // expected output: "Fire,Air,Water" console.log(elements.join('')); // expected output: "FireAirWater" console.log(elements.join('-')); – Harsh Lad Mar 19 '20 at 09:19