Angular bypassSecurityTrustResourceUrl saying any argument is undefined
Asked Answered
V

2

6

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!

Viguerie answered 4/1, 2019 at 16:26 Comment(0)
S
6

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) {    }
Sakovich answered 4/1, 2019 at 16:57 Comment(1)
I should've added this to the post but I already have the DomSanitizer imported and a DomSanitizer declared in my constructor. Sorry I left out some information in the post.Viguerie
V
0

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.

Viguerie answered 4/1, 2019 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.