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 a document.
Here is the old code:
loadScript(src:string){
const script = document.createElement('script');
document.body.appendChild(script);
script.src = src;
}
Now I would like to use Renderer2to avoid direct manipulation of the DOM. So, I entered 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 encounter this error:
Error: no provider for Renderer2!
The service belongs to CommonModule CoreModule, the only import of which is CommonModulefrom@angular/common
This plankre demonstrates the problem.
source
share