73
<!--HTML CODE-->
<p #mass_timings></p>

//Controller code

@ViewChild('mass_timings') mass_timings: ElementRef;
constructor(private domSanitizer:DomSanitizer)
getInnerHTMLValue(){
 this.mass_timings.nativeElement.innerHTML = 
   this.domSanitizer.bypassSecurityTrustHtml(this.parishDetail.mass_timings);

}

but the output which the mass_timings is displaying is including the text:-

Safe value must use [property]=binding

at the beginning

How to remove this string.

manish kumar
  • 4,412
  • 4
  • 34
  • 51

6 Answers6

86

As the error message says, the sanitized HTML needs to be added using property binding:

<p [innerHTML]="massTimingsHtml"></p>
constructor(private domSanitizer:DomSanitizer) {
  this.massTimingsHtml = this.getInnerHTMLValue();
}
getInnerHTMLValue(){
  return this.domSanitizer.bypassSecurityTrustHtml(this.parishDetail.mass_timings);
}

StackBlitz example (based on Swapnil Patwa's Plunker - see comments below)

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 2
    @manish kumar, Demo plunker - https://plnkr.co/edit/oCZ9yKTl68kuTPx6s7fH?p=preview – Swapnil Patwa Jul 27 '17 at 13:27
  • did this only. but can you relate to [this](https://stackoverflow.com/a/38826472/7260635) and explain – manish kumar Jul 27 '17 at 14:14
  • You can do that, but not with sanitized HTML, only with a plain HTML string. – Günter Zöchbauer Jul 27 '17 at 14:18
  • 1
    Not if you use `this.mass_timings.nativeElement.innerHTML = ...`. But sanitizing is recommended for security reasons. – Günter Zöchbauer Jul 27 '17 at 14:46
  • @manishkumar most of the time copying the Plunkers code to StackBlitz will fix it. Plunker based on old Angular versions don't run anymore. See the StackBlitz in my updated Answer. – Günter Zöchbauer May 17 '18 at 17:05
  • I've done this but it's still displaying the error message for some reason, anyone knows what I'm doing wrong? I'm literally using ```this.item = this.domSanitizer.bypassSecurityTrustHtml(this.item)``` and ```[innerHtml]="item"``` – SimbaClaws Apr 08 '20 at 12:26
  • I guess `this.item` is bound before `this.item = this.domSanitizer.bypassSecurityTrustHtml(this.item)` is executed. Try using a different field for `...this.domSanitizer.bypassSecurityTrustHtml(this.itemHtml)` – Günter Zöchbauer Apr 08 '20 at 12:40
  • is this the only way? What if we need to add a favicon programatically for example? – MartaGalve Jan 18 '21 at 05:03
  • @bubble Not sure what the problem is. This code is mostly about adding CSS or HTML programmatically. Perhaps you can demonstrate the problem in a stackblitz? – Günter Zöchbauer Jan 18 '21 at 12:33
  • I get the exact same error even though I'm doing this. –  Jul 10 '21 at 19:08
21

You need to sanitize() the safevalue like this :

this.domSanitizer.sanitize(SecurityContext.HTML,this.domSanitizer.bypassSecurityTrustHtml(this.parishDetail.mass_timings));
Sunil Kumar
  • 6,112
  • 6
  • 36
  • 40
20

I was getting this error when using an iframe so there I fixed using [src] as below:

Note: Use pipes for better performance

Import this:

import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
constructor(private sanitizer: DomSanitizer, ....other DI){}

In ts file

getSafeUrl() {
        return this.sanitizer.bypassSecurityTrustResourceUrl(this.url);     
}

In html file

<iframe [src]="getSafeUrl()" frameborder="0" *ngIf="url"></iframe>

This method is quite cycle consuming as it'll call the function multiple time so it's better to sanitize URL inside lifeCycleHooks like ngOnInit().

You can use pipes as well for better performance:

Using Pipe

HTML:

<iframe [src]="url | byPassSecurity"></iframe>

Sanitize.pipe.ts:

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

@Pipe({
    name: 'byPassSecurity'
})
export class ByPassSecurityPipe implements PipeTransform {

    constructor(private sanitizer: DomSanitizer) {}

    transform (value: string): SafeHtml {
        return this.sanitizer.bypassSecurityTrustHtml(value);
    }
}
Black Mamba
  • 13,632
  • 6
  • 82
  • 105
  • 5
    You just saved my time, thanks! src="{{code}}" doesn't work, but [src]="code" works like a charm! – Frank Jun 13 '18 at 15:25
  • 3
    After hours of trying, this was finally the solution which worked out for me. Anyone an idea why `src="{{code}}"` doesn't work but `[src]="code" ` does? – jammartin Jan 14 '19 at 09:41
  • 1
    You should use a pipe instead of a function to save performance and prevent it from being called every time the change detection runs. See Renil Babu answer. – Mick May 11 '20 at 14:11
  • how do you import the pipe?? – Zaffer Jul 31 '22 at 16:47
  • You'll need to import it inside the module of the component/page you want to use it in – Black Mamba Aug 06 '22 at 18:34
8

My Solution using Pipe.

HTML

<div [innerHtml]="htmlValue | byPassSecurity"></div>

Pipe

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

@Pipe({
    name: 'byPassSecurity'
})
export class ByPassSecurityPipe implements PipeTransform {

    constructor(private sanitizer: DomSanitizer) {}

    transform (value: string): SafeHtml {
        return this.sanitizer.bypassSecurityTrustHtml(value);
    }
}
Renil Babu
  • 2,118
  • 1
  • 20
  • 29
  • 1
    A pipe is the best solution in terms of performance since a function (like Black Mamba said) will be called each time the change detection runs. – Mick May 11 '20 at 14:13
  • Great job here, thank you sir! – Willie Jul 12 '23 at 21:04
5
You don't need to bind to [innerHtml] everywhere.

It works by using sanitize() and bypassSecurityTrustHtml() together like this :

this.mass_timings.nativeElement.innerHTML = (
    this.domSanitizer.sanitize(SecurityContext.HTML, this.domSanitizer.bypassSecurityTrustHtml(this.parishDetail.mass_timings))
);
luiscla27
  • 4,956
  • 37
  • 49
4

I got a same error while using MATHJAX in angular 7. I resolved by below pipe transform. 100% work perfectly

//Sanitize HTML PIPE

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

@Pipe({
    name: 'sanitizeHtml'
})
export class SanitizeHtmlPipe implements PipeTransform {

    constructor(private _sanitizer: DomSanitizer) {
    }

    transform(value: string): SafeHtml {
        return this._sanitizer.sanitize(SecurityContext.HTML, this._sanitizer.bypassSecurityTrustHtml(value))
    }
}
  • Why would you underscore sanitizer? Makes no sense. Also you say it works 100% perfectly but no thats not true, some others might use `SecurityContext.URL` for example depending on the context. – Mick May 11 '20 at 14:25
  • 1
    How to apply the same with title of an element – Shaik Habeeb Aug 14 '20 at 07:38