Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 display sanitized background image

I've been able to construct the image url without receiving the sanitizer errors, but my background image still will not show up. Do I have to access the "changingThisBreaksApplicationSecurity" property for this image to show properly? Any help with this is greatly appreciated.

enter image description here

Chrome DevTools

UPDATE: It seems wrong to access this property in the markup, but it works. enter image description here

like image 772
Jason Spence Avatar asked Sep 02 '25 15:09

Jason Spence


2 Answers

Reference the actual property changingThisBreaksApplicationSecurity since you need to get the url string.

like image 165
poxopox Avatar answered Sep 05 '25 05:09

poxopox


In order to get a row resouce URL of safeValue object, DomSanitizerImpl.sanitize() function may be used:

...
import { ɵDomSanitizerImpl, DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
...

// row URL 
url = 'https://www.gravatar.com/avatar/5ea793dcbaff5a5e29ad3d05ef564d86';

// safe value type URL
safeUrl = this._sanitizer.bypassSecurityTrustResourceUrl(this.url);

// sanitized back from safe value raw URL
sanitizedUrl = this._sanitizerImpl.sanitize(SecurityContext.RESOURCE_URL, this.safeUrl);

...
constructor(protected _sanitizer: DomSanitizer, protected _sanitizerImpl: ɵDomSanitizerImpl) {}

I created a STACKBLITZ to demonstrate it, please look at console.logs

like image 32
Andriy Avatar answered Sep 05 '25 05:09

Andriy