How to call JavaScript functions from Typescript in Angular 5?
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.
.angular-cli.json
2. How to call the JavaScript Functions from Typescript class?
your TS should be like this.
myJsFile.js file content.
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);
}
Ravi
Full Stack Developer. (Java+Spring Boot+Angular+AWS Cloud) Life Long Learner
Updated on July 09, 2022Comments
-
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:
- How and where to include JavaScript Files in Angular Project?
- How to call the JavaScript Functions from Typescript class?
It would be great if anyone provides me an example !!
Thanks in Advance!!
-
Billy Koswara over 5 yearsWorking with angular 6 too
-
Billy Koswara over 5 yearsI 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 over 5 yearsIf 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 almost 5 yearsHow to pass any parameter in the same method of external file?
-
so-random-dude over 4 yearsWorks for Angular 8. Only change is, instead of .angular-cli.json its just angular.json
-
arunraj6 over 4 yearsPlease check the latest update in my answer with an example of Angulr8.
-
Akhil over 4 yearsdidn't work for me. I had to pass one parameter. This didn't show any error but that function isn't alerted
-
Shashikant Devani over 4 yearsHope, 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 over 4 yearsYes 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 over 4 yearsinstead of "var", you should write "function" keyword.
-
Yesha Shah about 4 yearsHow to call function having parameters from .js file in .ts file? @ArunRajR
-
Yesha Shah about 4 yearsHow to call function having parameters from .js file in .ts file?@ShashikantDevani
-
Juliyanage Silva about 4 years@YeshaShah yeah I need the same and seems it is not working
-
Shashikant Devani about 4 years@YeshaShah, Use
var
instead offunction
. Like thisdeclare var myfunction: any;
-
Shashikant Devani about 4 years@YeshaShah, Hope you had added your custom created js file in
scripts
inangular.json
. Like thissrc/assets/js/custom.js
. If you added custom.js path in running project than restart project(ng serve or npm start)
after adding anything inangular.json
. -
N4ppeL almost 4 yearstypescript complains about that. Using
declare function myfunction(param1: string, param2: string): any;
worked.. -
user2746732 over 3 yearsdeclare function myfunction: any; doesn't work in type script neither declare function myfunction(param1: string, param2: string): any;
-
jkr over 3 yearsdeclare function myfunction(parameter1: any): any; works
-
Sarath Mohandas about 3 yearsWorking perfectly with Angular 10 and 11 too.
-
arunraj6 about 3 years@YeshaShah passing the parameters is explained in Shashikant Devani answer.
-
Venugopal M about 3 yearsWorks great with Ang8. No tweaks required except for the json file name
-
FiringBlanks almost 2 yearsI was getting an error saying the JS function is not defined, but it's working now after restarting my localhost server.