Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the right way unescape html entities in Angular?

I get html entities from json file, like: ’ How can I unescape it in html component?

I created custom pipe, but it works only for entities like &:

import { Pipe, PipeTransform } from '@angular/core';
import {unescape} from 'lodash';

@Pipe({
  name: 'unescape'
})
export class UnescapePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    return unescape(value);
  }

}
  • I'm working with Angular 5.
like image 351
Adam Pery Avatar asked Nov 19 '17 14:11

Adam Pery


1 Answers

The solution, create next custom pipe:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'unescape'
})
export class UnescapePipe implements PipeTransform {

  transform(value: any, args?: any): any {
    const doc = new DOMParser().parseFromString(value, 'text/html');
    return doc.documentElement.textContent;
  }

}
like image 180
Adam Pery Avatar answered Oct 08 '22 18:10

Adam Pery