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:
It would be great if anyone provides me an example !!
Thanks in Advance!!
Until then, you will need some interoperability between JavaScript and TypeScript. Being in a JS file and calling a function defined in a . ts is trivial - it just works™.
TypeScript is a primary language for Angular application development. It is a superset of JavaScript with design-time support for type safety and tooling. Browsers can't execute TypeScript directly. Typescript must be "transpiled" into JavaScript using the tsc compiler, which requires some configuration.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With