Home >Web Front-end >JS Tutorial >How to Integrate External JavaScript Scripts and Functions into Your Angular Application?
Incorporating JavaScript Scripts and Invoking Functions in Angular
To include an external JavaScript file in Angular and utilize functions within it, follow these steps:
Reference the Script File:
In the angular-cli.json file (or angular.json for Angular 6 ), add the following code:
<code class="JSON">"scripts": [ "../path" ]</code>
Declare a Variable in typings.d.ts:
Create a typings.d.ts file in the src directory if it doesn't exist, and then add the following line:
<code class="TypeScript">declare var variableName: any;</code>
Import the Script:
In the component where you want to use the JavaScript function, import the variable you declared in typings.d.ts:
<code class="TypeScript">import * as variable from 'variableName';</code>
Invoking the Function:
You can now access the JavaScript function from the imported variable. For example, to call the xyz() function from the abc.js script, you would write:
<code class="TypeScript">variable.xyz();</code>
By following these steps, you can easily include JavaScript script files in Angular applications and seamlessly integrate their functionality.
The above is the detailed content of How to Integrate External JavaScript Scripts and Functions into Your Angular Application?. For more information, please follow other related articles on the PHP Chinese website!