3

Under index.html I have delcared an external js file like
<script data-siteid="XXXXXX" src="https://api.cartstack.com/js/customer-tracking/website.com_c7ee604471301b0bd028e8f19a535557.js" type="text/javascript"> </script>

I have to call a function named cartstack_updatecart() from a component. (That function is generating from above link). How to do that?

1 Answer 1

6

Follow below steps in your component:-

1) First add a reference of your external JS file for importing it to the component. 
   Import * as abcJS from '/YourExternalJS.js';

2) Now declare a "var" of the same name that your function has inside external JS.
   declare var cartstack_updatecart: any;

3) ngOninit(){
 cartstack_updatecart();
 }

4) Do remember that your JQuery should be loaded first than your external JS file.

I have called that function on ngoninit lifecycleHook. Also instead of declaring external JS file inside Index.html, I would rather suggest you to declare it in angular-cli.json file in scripts array. All the best.

Sign up to request clarification or add additional context in comments.

6 Comments

@Dipankar Naskar Have you got the solution or you still facing issues? Please let me know, I can share the code in more detail.
yeah I have tried your suggestion. and it's working. Thanks man.
Just quick note for me it worked without first step.. That just gave me error '../../yourExternalJS.js' is not a module (it worked but the error was in the bash).. and import in angular-cli.json was also required. So in my case, skip first step and add another step which is add script in angular-cli.json scripts..
Yes scripts can be included either via adding them to angular-cli.json or dropping their reference straight forward to Index.html. I would also like to notify here that in angular 6, we don't have angular-cli.json. CLI projects in angular 6 onwards will be using angular.json instead of .angular-cli.json for build and project configuration.
Hi @AbhishekSharma, in my case JS file is deployed on another server. How can I achieve the same thing?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.