Solution 1 :

If you want to display new line in template you can use <br> tag for new line.
And in html file use innerHtml property for binding data for p tag

Try this

.ts

this.s = this.detail.split(',').join('<br />')

.html

<p [innerHtml]="s"></p>

Solution 2 :

When you split the string – you have an array of the component parts of the string – simply iterate over these with an ngFor and use a block level element like a p element to automatically puth them on separate lines (or use a ul with the details in li’s which again are block level elements and will render on new lines).

getdetails(x:any){
this.detail = x.details;
this.s = this.detail.split(','); // creates an array you can iterate over 
}

<p *ngFor="let _s of s">{{ _s }}</p>

Solution 3 :

this.s = this.detail.split(',');
and in the template
<p *ngFor="let _s of s">{{_s}}</p>

Solution 4 :

Use pre tag to defines preformatted text.

<div class="col-md-4 col-12 overflow-auto test">
      <div class="alert alert-dark">No description yet <span class="font-weight-bold">+Add Description</span> </div>
      <p>{{s}}</p>
  </div>

Example

Solution 5 :

You try:

<p style="white-space: pre-line;">{{s}}</p>

Problem :

I need to split the string with a comma to a newline. I have tried using split and join function. It is removing commas only but not printing on a new line.

string='Lorem Ipsum,Lorem Ipsum,Lorem Ipsum'

My code is printing like this in HTML:

Lorem Ipsum Lorem Ipsum Lorem Ipsum 

It’s supposed to print like this:

Lorem Ipsum
Lorem Ipsum
Lorem Ipsum

Code:

{
  'date':'Nov 12',
  'name':"Agra",
  'entities': 14,
  'details':'Lorem Ipsum,Lorem Ipsum,Lorem Ipsum'
}

getdetails(x:any){
  this.detail = x.details;
  this.s = this.detail.split(',').join('n')
}
    <div class="col-md-4 col-12 overflow-auto test">
      <div class="alert alert-dark">No description yet <span class="font-weight-bold">+Add Description</span> </div>
      <p>{{s}}</p>

    </div

stackblitz link

Comments

Comment posted by ROOT

try to have like this instead,

Comment posted by jonrsharpe

Why not put each part of the split in a separate

Comment posted by Prem

printing like this Lorem Ipsum
Lorem Ipsum
Lorem Ipsum

Comment posted by Prem

Error; Cannot find a differ supporting object ‘Lorem Ipsum Lorem Ipsum Lorem Ipsum’ of type ‘string’. NgFor only supports binding to Iterables such as Arrays.

Comment posted by gavgrif

sorry – that might be me messing with your structures in the snippet – solution edited. i personally would do it the original way i did it – but it also needs to fit your data structure and typescript definitions.

By