Im aware there are similar questions like so.. domSanitizer and WARNING: sanitizing unsafe style value url I have done what has been said in those and I cant seem to get it to work...
app.component.html
<div *ngIf="item?.fields?.asset[0]?.fields?.backgroundImage" class="program-item_background" [style.background]=backgroundImage></div>
app.component.ts
import { DomSanitizer, SafeResourceUrl, SafeUrl, SafeStyle } from '@angular/platform-browser';
export class ProgramItemComponent implements OnInit, OnChanges, Sanitizer {
@Input() item: Entry<any>; // Program Item getting passed down from the parent
backgroundImage: SafeStyle;
constructor(
private sanitization: DomSanitizer
) { }
ngOnChanges(changes: SimpleChanges) {
this.backgroundImage = this.safeStyle(this.item.fields.asset[0].fields.backgroundImage.fields.file.url);
}
safeStyle(url){
return this.sanitization.bypassSecurityTrustStyle(`'url(${url})'`);
}
I'm also getting this error in the console..
but I think thats because it couldnt find the backgroundImage oninit.. due to it being pulled in from an external api
any help would be appreciated! Thanks
EDIT
I have also tried this...
ngOnChanges(changes: SimpleChanges) {
this.backgroundImage = this.sanitization.bypassSecurityTrustStyle(`'url(${this.item.fields.asset[0].fields.backgroundImage.fields.file.url})'`);
}
but that doesnt seem to working either