0

How should I add a script to a component view in Angular ? if I write it in the component.html it disapears in the view source of the html page , but I managed to add the script with DOM sanitizer and I can see the script tag in the source code of the page when I do the view source , but angular simply ignores it.

here is the script that I want to add to the page

<script 
  type='text/javascript' 
  src='https://test-www.payson.se/embedded/Content/payson.js?v2'>
</script>
Yashar
  • 1,122
  • 2
  • 15
  • 43
  • Why not simply put the code of your script into the code of your component ? ... –  Aug 24 '18 at 12:30
  • @trichetriche It is a third party script and have to be added to the view , not any other place like index.html – Yashar Aug 24 '18 at 12:32
  • We're going to need more information than that. –  Aug 24 '18 at 12:33
  • Does the script have an npm package you can import? Or can you pull the script into its own file, do a little modification, then use its code from there? Need more information about the script. – canpan14 Aug 24 '18 at 12:34
  • @edited the question – Yashar Aug 24 '18 at 13:08
  • it doesnt make any sense to say that the script can't be loaded anywhere except the view. The browser doesn't have a concept of view scope with the script tag. – bryan60 Aug 24 '18 at 13:14
  • @bryan60 I will get that script inside the component as a response to a httpRequest. so I don't have in any other place from the begining – Yashar Aug 24 '18 at 17:27
  • if the issue is that the address of the script itself isn't known to anyone but the component, then you can just use something like let scriptTag = document.createElement('script'); scriptTag.src = mySource; document.head.appendChild(scriptTag); Numerous dynamic script loading methods here: https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – bryan60 Aug 25 '18 at 18:18

0 Answers0