I need to remove unsafe from my url: unsafe:blob:http://localhost:1234/example, Below is what I did.
Script:
import {DomSanitizationService} from '@angular/platform-browser';
class example{
public DomSanitizer: DomSanitizationService;
public url;
constructor() {
this.url = 'www.example.com';
}
}
HTML:
<a href={{DomSanitizer.bypassSecurityTrustHtml(url)}}>
However, it gives me error: TypeError: Cannot read property 'bypassSecurityTrustHtml' of undefined. I checked DomSanitizationService in @angular/platform-browser, it is an abstract class and cannot be instantiated. What is the right way to call? I saw a lot of answer like this (How to avoid adding prefix “unsafe” to link by Angular2?), the DomSanitizationService is passed from constructor but I don't understand when instantiated the class, what should be passed in. Besides, I don't want to change the contract of constructor. I would like to know what is the right way to achieve my goal. I am using angular2.
Update: Now I am able to make it work by injecting DomSanitizationService through constructor, however, in my test, I need to instantiate my component and what should be passed in for DomSanitizationService?