Skip to content
Advertisement

Calling JavaScript from component template

I’m working on integrating payment forms from a third party, by using the Link to Javascript they provide.

when I place the code in the index.html page its works just fine, but when I move the code to the component template is not working, below is part of the code:

this is the script code to initialize the payment form:

JavaScript
JavaScript

when I’m using these three-part together on the index page is working fine when I’m trying to work from a component template that is not

Advertisement

Answer

Put the script tag <script type="text/javascript" src="http://xxx/tokenPay.js"></script> inside the head tag within index.html

Then put the var tokenpay = TokenPay('tokenpay123'); etc block inside ngAfterViewInit within the relevant component (i.e. the component hosting the form). Then it should work fine.

Note that because tokenPay is a global variable, the TypeScript compiler will not recognise it so you will need to add:

declare const tokenPay: any

inside the .ts file of the relevant component.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement