0

Im trying to inject a JS in a Angular Component Library(not a app) for this the only solution i found was to create a Injectable class and then in its constructor i have called a method which :

if(typeof google == 'undefined'){
let script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://www.gstatic.com/charts/loader.js';
document.getElementsByTagName('head')[0].appendChild(script);}

And as you can guess, as soon as this runs, the Angular DOMSanitizer runs which i agree is right, but how do i solve this?

Also the Docs of DOMSanitizer hardly point out anything...

Pratik Kelwalkar
  • 1,592
  • 2
  • 15
  • 21
  • are you trying to insert external js in angular is that so? – Rahul Singh Aug 11 '17 at 07:45
  • yes, i want my component library module to remove the burden of the consuming application developer to insert those js – Pratik Kelwalkar Aug 11 '17 at 07:52
  • i dont get you you want to just insert that js then what is the problem check this answer of importing 3rd party js - https://stackoverflow.com/a/45387777/2708210 – Rahul Singh Aug 11 '17 at 07:59
  • i guess u didnt get the point, I do not have a angular-cli.json, i have a standalone library...this wont help – Pratik Kelwalkar Aug 11 '17 at 09:07
  • Please, specify why exactly is the problem with DOM sanitizer. Yes, it's supposed to be done like that (more or less), and running the code in constructor will make the script inserted each time the component is compiled. – Estus Flask Aug 11 '17 at 09:33
  • yes it would add the script every time until a check is provided, i have used this one `typeof google !== 'undefined' && google.charts` and if this is false then i want to add the script. The Domsanitizer simply removes my script tag – Pratik Kelwalkar Aug 11 '17 at 10:23

0 Answers0