Angular bypassSecurityTrustResourceUrl saying any argument is undefined

Viewed 5946

I'm trying to sanitize a URL using bypassSecurityTrustResourceUrl. However, regardless of what value I put in the argument I always get

Cannot read property bypassSecurityTrustResourceUrl of undefined

return this.sanitizer.bypassSecurityTrustResourceUrl('http://www.google.com');

Returns:

ERROR TypeError: Cannot read property bypassSecurityTrustResourceUrl of undefined at UrlSanitizerService.push../src/app/services/urlsanitizer.service.ts.UrlSanitizerService.sanitizeURL

Everywhere I've look says this should sanitize a url and allow it to be displayed via iframe. I may be misunderstanding what to put in for an argument. Any help would be appreciated, thank you!

2 Answers

This error is not about what you pass as an argument, but about the sanitizer.

You need to make sure to import and declare the sanitizer in your constructor like this:

import { DomSanitizer} from '@angular/platform-browser';

...

constructor(private sanitizer:DomSanitizer) {    }

Update: I figured out the problem. I'm not sure why this was the case but when I moved the sanitizer from a service to my app.component it everything worked fine.

Related