How to replace HTML body to text body including line break in angular?

Viewed 23

My HTML Content is like this: "<p>Hi&nbsp;Bhavna,</p><p>&nbsp;</p> <p>You are invited to give the&nbsp;employee survey 2022 survey. To start the survey please click on below link.</p> <p>&nbsp;</p> <p><a href='http://localhost:4401/survey/384/eskl60ke5nk' target='_blank'>Click here.</a></p>"

which I want it to be like this using angular.

Hi Bhavna,

You are invited to give the employee survey 2022 survey. To start the survey please click on below link.

Click here.

1 Answers

You can do this in Angular by using [innerHTML] property binding , but make sure you call bypassSecurityTrustHtml over sanitizer dependency. Consider creating safehtml pipe for the same

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'safeHtml'
})
export class SafeHtmlPipe implements PipeTransform {

 constructor(protected sanitizer: DomSanitizer) {}

 public transform(value: any, type: string): any {
     return this.sanitizer.bypassSecurityTrustHtml(value);
  }
}

HTML

<div [innerHTML]="html | safeHtml"></div>

Stackblitz

Related