0

I'm using Angular-10 and created one shared function as below (inside dom.ts file):

export function toSafeHtml(value) : any {
  return new DOMParser().parseFromString(value, "text/html").documentElement.textContent;
}

And I want to call this wherever I have a [innerHtml] attributes. For example :

<div style="margin: 0 auto;">
    <mat-spinner></mat-spinner>
    <div [innerHtml]="toSafeHtml(loaderTitle)"></div>
</div>

How to do this? Is there any better way to call the shared function directly in DOM?

3
  • What do you exactly want. and what your intension about directly in DOM Commented Mar 24, 2021 at 7:25
  • @er-sho I mean like this [innerHtml]="toSafeHtml(loaderTitle)" or any better way Commented Mar 24, 2021 at 7:30
  • I have posted my answer. kindly look into it and let me know Commented Mar 24, 2021 at 7:38

4 Answers 4

2

Another solution is to create a pipe.

safe-html.pipe.ts

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

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

  constructor() {}
  transform(value: any) {
    return new DOMParser().parseFromString(value, 'text/html').documentElement.textContent;
  }
}

html

<div style="margin: 0 auto;">
    <mat-spinner></mat-spinner>
    <div innerHtml="{{loaderTitle | safeHtml}}"></div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

Can you check whether it is helpful?.

import { Component } from '@angular/core';

import * as fun from './function';

@Component({
  selector: 'my-app',
  template: `<p [innerHTML]="myFun.toSafeHtml(loaderTitle)"></p>`,
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  myFun = fun;
  loaderTitle = 'my-text'

  constructor() {
  }
}

and the function.ts in the same directory

export function toSafeHtml(value) : any {
  return new DOMParser().parseFromString(value, "text/html").documentElement.textContent;
}

2 Comments

I don't want repeat the same for each and every component.
As I know HTML files are conned to the app through component.ts file. So, we have to update the conponent.ts if we use it in HTML
0

You have to write wrapper method in your component ts file.

your.component.ts

wrapperToSafeHtml(value) {
  return toSafeHtml(value);
}

your.component.html

<div style="margin: 0 auto;">
    <mat-spinner></mat-spinner>
    <div [innerHtml]="wrapperToSafeHtml(loaderTitle)"></div>
</div>

2 Comments

@n-f But, in this case I have to write wrapper method in every component. It's repetitive correct?
Yes, it is correct. You can not call function from html.
0

You can do the one trick here, so you don't need to write your toSafeHtml function again and again,

First create one service like

import { Injectable } from "@angular/core";

@Injectable({
  providedIn: "root",
})
export class YourServiceName {

  toSafeHtml(value): any {
    return new DOMParser().parseFromString(value, "text/html").documentElement.textContent;
  }
}

Then you have to add dependency in constructor of your component where you need to use above function

constructor(public yourServiceName: YourServiceName) {  
} 

And then you can use it in you template like

<div [innerHtml]="yourServiceName.toSafeHtml(loaderTitle)"></div>

Edit 1:

I don't want repeat the same for each and every component

You can also create a attribute directive to achieve your goal

import { Directive, ElementRef, HostListener, Input, OnInit } from '@angular/core';

@Directive({
  selector: '[appHtmlParser]'
})
export class HtmlParserDirective implements OnInit {

   @Input('html') html: any;
 
   constructor(private el: ElementRef) {    
   }

   ngOnInit(): void {
    this.el.nativeElement.textContent = this.toSafeHtml(this.html);
   }

   toSafeHtml(value): any {
        return new DOMParser().parseFromString(value, "text/html").documentElement.textContent;
   }
}

And then you can use it in your template like

<div appHtmlParser [html]="loaderTitle"></div>

Stackblitz Demo

2 Comments

I don't want repeat the same for each and every component.
@ShreekumarS, I have added an alternative, Kindly view Edit 1 section.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.