Currently I have an Angular component that appends a list of script tags into the DOM with Renderer2 library, the code of these scripts is retrieved from an external source. In the following snippet, scripts array is the list of source links. Being this the case, I'm unable to modify the JS code from them:
for(let i = 0; i<scripts.length; i++){
let script = this.renderer2.createElement('script');
script.type = `text/javascript`;
script.src = scripts[i];
this.appendedChildren.push(script);
this.renderer2.appendChild(this._document.body, script);
}
I tried to remove them from the DOM, but the scripts keep executing:
ngOnDestroy(){
for(let i = 0; i<this.appendedChildren.length; i++)
this.renderer2.removeChild(this._document.body, this.appendedChildren[i]);
}
What I would like is to get the pid or some kind of identificator to be able to kill the JS scripts within the ngOnDestroy(), and also a method to do it so.