Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse HTML from response in Angular?

I do request to server and get JSON result:

{"result" => "HTML code"}

How to parse HTML code and get table from this response?

I tried to place this code to hidden block on the page:

<div #content>HTML code here from response</div>

What next? How to parse?

like image 308
OPV Avatar asked Dec 05 '22 09:12

OPV


1 Answers

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

@Pipe({name: 'sanitizeHtml'})
export class SafeHtmlPipe implements PipeTransform {
   constructor(private sanitized: DomSanitizer) {
   }
   transform(value: string) {
     return this.sanitized.bypassSecurityTrustHtml(value);
   }
}

Usage:
<div [innerHTML]="content | sanitizeHtml"></div> //Content is what you got from json

We should always santize the content to prevent any malicious activity with DOMSanitizer. So for that We can create a pipe as above and use it anywhere in app.

like image 200
Siddharth Pal Avatar answered Jan 06 '23 01:01

Siddharth Pal