1

Hi i need to call a normal javascript function from a typescripit file.

Assume that i have a javascript file names test.js and i have imported this file in the index.html of the application.

Now i need to call this test() function from app.component.ts file.

function test(){

cosole.log('test');

}
Mistalis
  • 17,793
  • 13
  • 73
  • 97
  • Possible duplicate of [Angular2 - how to call component function from outside the app](https://stackoverflow.com/questions/35296704/angular2-how-to-call-component-function-from-outside-the-app) – Mistalis Jun 21 '17 at 07:05
  • where is that JavaScript function ,i mean write some other component ? – yala ramesh Jun 21 '17 at 07:44
  • Just look in following answer. [https://stackoverflow.com/a/37348233/7880063](https://stackoverflow.com/a/37348233/7880063) – Ravikumar Jun 21 '17 at 13:02

2 Answers2

6

This works for me

(window as any).myJavascriptFunction()
Croeber
  • 451
  • 3
  • 10
  • 22
2

I think there is no default way to in JavaScript. What you can do is write your JS file like this.

className/test.js

class className {
   function test() {
     console.log('test');
}
exports.default = className;

Create a Type definition.

className/index.d.ts

export default class className {
   test(): void;
}

You can now use your JS in app.component.ts.

import className from "./className";

var t = new className();
t.test();
Sriram Jayaraman
  • 800
  • 1
  • 8
  • 15