close

How do I include a JavaScript script file in Angular and call a function from that script?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I include a JavaScript script file in Angular and call a function from that script? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I include a JavaScript script file in Angular and call a function from that script ?

  1. How do I include a JavaScript script file in Angular and call a function from that script?

    do I include a JavaScript script file in Angular and call a function from that script js file in the scripts array from angular-cli.json (angular.json when using angular 6+):
    "scripts": [ "../node_modules/jquery/dist/jquery.js" ]
    After this, restart ng serve if it is already started.

  2. How do I include a JavaScript script file in Angular and call a function from that script?

    do I include a JavaScript script file in Angular and call a function from that script js file in the scripts array from angular-cli.json (angular.json when using angular 6+):
    "scripts": [ "../node_modules/jquery/dist/jquery.js" ]
    After this, restart ng serve if it is already started.

Method 1

Refer the scripts inside the angular-cli.json (angular.json when using angular 6+) file.

"scripts": [
    "../path" 
 ];

then add in typings.d.ts (create this file in src if it does not already exist)

declare var variableName:any;

Import it in your file as

import * as variable from 'variableName';

Method 2

In order to include a global library, eg jquery.js file in the scripts array from angular-cli.json (angular.json when using angular 6+):

"scripts": [
  "../node_modules/jquery/dist/jquery.js"
]

After this, restart ng serve if it is already started.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment