How to call JavaScript functions from Typescript in Angular 5?

110,996

Solution 1

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

Solution 2

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);
}
Share:
110,996
Ravi
Author by

Ravi

Full Stack Developer. (Java+Spring Boot+Angular+AWS Cloud) Life Long Learner

Updated on July 09, 2022

Comments

  • Ravi
    Ravi almost 2 years

    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 !!

    Thanks in Advance!!

  • Billy Koswara
    Billy Koswara over 5 years
    Working with angular 6 too
  • Billy Koswara
    Billy Koswara over 5 years
    I wonder about the performance. Does include Js file in angular.json the best way. does it loaded and ready for all component to call. in fact you use declare function:any is kind of weird, it means i can call the function like that in other component too right? won't it affect the performance?
  • arunraj6
    arunraj6 over 5 years
    If you really worried about the perfomance & declaration syntax in the component(one/multiple). You can go for TypesScript declaration files. Conside this sample example: bennadel.com/blog/…
  • Pardeep Jain
    Pardeep Jain almost 5 years
    How to pass any parameter in the same method of external file?
  • so-random-dude
    so-random-dude over 4 years
    Works for Angular 8. Only change is, instead of .angular-cli.json its just angular.json
  • arunraj6
    arunraj6 over 4 years
    Please check the latest update in my answer with an example of Angulr8.
  • Akhil
    Akhil over 4 years
    didn't work for me. I had to pass one parameter. This didn't show any error but that function isn't alerted
  • Shashikant Devani
    Shashikant Devani over 4 years
    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
  • Akhil
    Akhil over 4 years
    Yes sorry not to update my comment. I had to add the myscript.js file location in <script> tag of my index.html. That way it worked. Thanks @Shashikanth
  • Dila Gurung
    Dila Gurung over 4 years
    instead of "var", you should write "function" keyword.
  • Yesha Shah
    Yesha Shah about 4 years
    How to call function having parameters from .js file in .ts file? @ArunRajR
  • Yesha Shah
    Yesha Shah about 4 years
    How to call function having parameters from .js file in .ts file?@ShashikantDevani
  • Juliyanage Silva
    Juliyanage Silva about 4 years
    @YeshaShah yeah I need the same and seems it is not working
  • Shashikant Devani
    Shashikant Devani about 4 years
    @YeshaShah, Use var instead of function. Like this declare var myfunction: any;
  • Shashikant Devani
    Shashikant Devani about 4 years
    @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.
  • N4ppeL
    N4ppeL almost 4 years
    typescript complains about that. Using declare function myfunction(param1: string, param2: string): any; worked..
  • user2746732
    user2746732 over 3 years
    declare function myfunction: any; doesn't work in type script neither declare function myfunction(param1: string, param2: string): any;
  • jkr
    jkr over 3 years
    declare function myfunction(parameter1: any): any; works
  • Sarath Mohandas
    Sarath Mohandas about 3 years
    Working perfectly with Angular 10 and 11 too.
  • arunraj6
    arunraj6 about 3 years
    @YeshaShah passing the parameters is explained in Shashikant Devani answer.
  • Venugopal M
    Venugopal M about 3 years
    Works great with Ang8. No tweaks required except for the json file name
  • FiringBlanks
    FiringBlanks almost 2 years
    I was getting an error saying the JS function is not defined, but it's working now after restarting my localhost server.