5

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!

Milo
  • 3,365
  • 9
  • 30
  • 44
Eric Petrine
  • 53
  • 1
  • 4

2 Answers2

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) {    }
Uğur Dinç
  • 2,415
  • 1
  • 18
  • 25
  • 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. – Eric Petrine Jan 04 '19 at 17:01
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.

Eric Petrine
  • 53
  • 1
  • 4