Home >Web Front-end >JS Tutorial >How to Include JavaScript Script Files and Call Functions in Angular?
Including JavaScript Script File and Calling Functions in Angular
In Angular projects, integrating external JavaScript scripts and accessing their functions can enhance application capabilities. A common scenario is incorporating a script with a public function to extend Angular's functionality. Here's a comprehensive guide on how to achieve this:
Including the Script File
To include the JavaScript file in Angular, modify the "scripts" section of the angular-cli.json (or angular.json for Angular 6 ). Add the path to the script file within the array as shown below:
<code class="json">"scripts": [ "../path/to/abc.js" ]</code>
Referencing the Script in TypeScript
In your TypeScript code, create a declaration file named typings.d.ts if it doesn't exist. Add a declaration like this:
<code class="typescript">declare var variableName: any;</code>
Importing and Calling the Function
To import the script in your component or service, use the following syntax:
<code class="typescript">import * as variable from 'variableName';</code>
Now, you can call the function from the imported variable, for example:
<code class="typescript">variable.xyz();</code>
By following these steps, you can easily integrate external JavaScript scripts and utilize their functions within your Angular applications.
The above is the detailed content of How to Include JavaScript Script Files and Call Functions in Angular?. For more information, please follow other related articles on the PHP Chinese website!