7

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 2

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) {    }
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.