Angular 4.2 with Typescript 2.3
I am refactoring a service that is responsible for creating a new script tag and adding it to the document.
Here is the old code:
loadScript(src:string){
const script = document.createElement('script');
document.body.appendChild(script);
script.src = src;
}
Now, I'd like to use the Renderer2 to avoid doing direct DOM manipulation. So I've injected what I need in my service and updated the code:
constructor(private renderer:Renderer2, @Inject(DOCUMENT) private document){}
loadScript(src:string){
const script = this.renderer.createElement('script');
this.renderer.appendChild(this.document.body,script);
script.src = src;
}
However, I run into this error:
Error: no provider for Renderer2!
The service belongs to a CoreModule whose only import is CommonModule from @angular/common
This plunkr demonstrates the problem