0

I'm trying to include "parameters.js" file into my angular 4 project.

You can see below my "parameter.js" file content :

window.myKey = "http://webserviceUrl/"

I want to include this file in my project and get "myKey" value in a typescript file before loading web service datas. My "parameters.js" file as a specific structure and do not be changed (no json :'( ).

Is anyone as loading js into typescript with angular 4 ? How can i do that ?

Thanks for your help.

2
  • Can you not simply include that file in index.html? Commented Jun 9, 2018 at 6:13
  • Maybe... but how can i use my global variable in typescript. When i do : console.log('This my value'+myKey); Angular display an error because this variable is not know. Commented Jun 9, 2018 at 9:00

3 Answers 3

1

Include the script in index.html

<script src="/assets/parameters.js" />

And declare the variable in the component where you need to access it

declare let myKey: string;
Sign up to request clarification or add additional context in comments.

Comments

0

According to David explainations, i have added my parameters script in my angular conf ".angular-cli.json" like this :

"scripts": [
    "../directory/parameters.js"
],

I can read the value in typescript with the code :

declare let myKey: string;

Thank you David for you great response ! :)

Comments

0

You can Import js locally to ts file like this : import '../../../scripts/custom.js';

then declare method name in custom.js that you want to use like this 👍 declare var fAlert; (declarations statements are usually put just after the import statements) then in a place like ngOnInit directly use this method

fAlert();

Comments

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.