45

I'm working on PDF Viewer development in Angular 5. I'm done with writing HTML code for the UI part. Now I've JavaScript files that provide functionality for the UI elements. As Angular 5 supports typescript for implementing functionality for UI components, I want to include JavaScript files in Angular Project and call them from my Typescript code.

Questions:

  1. How and where to include JavaScript Files in Angular Project?
  2. How to call the JavaScript Functions from Typescript class?

It would be great if anyone provides me an example.

starball
  • 20,030
  • 7
  • 43
  • 238
Ravi
  • 1,614
  • 4
  • 14
  • 27

2 Answers2

135

1. How and where to include JavaScript Files in Angular Project?

You need to include your JS file in the asset folder and refer this JS file in .angular-cli.json file.

Refer the snapshot's,

Folder structure should be like this.

enter image description here

.angular-cli.json

enter image description here

2. How to call the JavaScript Functions from Typescript class?

your TS should be like this.

myJsFile.js file content.

enter image description here

This sample logic mentioned above will work, I have tried and tested using version 4, So I am expecting it to work with version 5 also.

Updating the answer for the new Angular version release. Working perfectly till version 11.2.6.

Find the code (function with parameter) here for Angular11

Arun Raj R
  • 2,197
  • 3
  • 13
  • 23
15

The given answer by Arun Raj R is perfect.

But for the angular 6+ project you need to take angular.json instead of angular-cli.json.

also if you want to pass parameter inside external function that come from js file than use just function name.

For Ex :

app.component.ts

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
declare function myfunction: any; // just change here from arun answer.

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.scss']
})
export class TestComponent implements OnInit {
    constructor() { }

    ngOnInit() { 
        myfunction('test1', 'test2');
    }
};

yourJs file :

function myfunction(params1, params2) {
    console.log('param1', params1);
    console.log('param2', params2);
}
Dila Gurung
  • 1,726
  • 21
  • 26
Shashikant Devani
  • 2,298
  • 1
  • 12
  • 25
  • didn't work for me. I had to pass one parameter. This didn't show any error but that function isn't alerted – Akhil Nov 21 '19 at 05:51
  • Hope, you added that javascript file inside the angular.json. also stop running the ng serve and again start it. So angular cli fetch updated angular.json – Shashikant Devani Nov 21 '19 at 05:58
  • Yes sorry not to update my comment. I had to add the myscript.js file location in – Akhil Nov 21 '19 at 07:46
  • instead of "var", you should write "function" keyword. – Dila Gurung Jan 02 '20 at 08:58
  • 1
    How to call function having parameters from .js file in .ts file?@ShashikantDevani – Yesha Shah Feb 07 '20 at 10:00
  • @YeshaShah yeah I need the same and seems it is not working – Juliyanage Silva Feb 07 '20 at 15:41
  • @YeshaShah, Use `var` instead of `function`. Like this `declare var myfunction: any;` – Shashikant Devani Feb 08 '20 at 16:46
  • @YeshaShah, Hope you had added your custom created js file in `scripts` in `angular.json`. Like this `src/assets/js/custom.js`. If you added custom.js path in running project than restart project `(ng serve or npm start)` after adding anything in `angular.json`. – Shashikant Devani Feb 08 '20 at 16:57
  • typescript complains about that. Using `declare function myfunction(param1: string, param2: string): any;` worked.. – N4ppeL Jun 10 '20 at 16:44
  • declare function myfunction: any; doesn't work in type script neither declare function myfunction(param1: string, param2: string): any; – user2746732 Jan 09 '21 at 13:51
  • declare function myfunction(parameter1: any): any; works – jkr Jan 19 '21 at 10:28