2

I am using VS 2015 as my IDE. I have read the following: Angular 2 typescript invoke javascript function

and

Using a Javascript Function from Typescript

and

Using a Javascript Function from Typescript

however, i still don't get the basic.

assuming that i have testing.js and app.component.ts

    var testJs = function () {
    this.asd = 123;

}

testJs.prototype.testing = function (param) {
    console.log(param);
}

How do I use testJs in my app.component.ts? at the moment i try this but failed:

import { Component } from '@angular/core';
interface testJs {
    testing: Function;
}
declare var testJs: testJs;
@Component({
    selector: 'my-app',
    templateUrl: '/mainTemplate.html'
})
export class AppComponent {
    testJs.testing("adsf");
}

I have tried the following and it still does not work as well. enter image description here

and I don't have angular.cli. is it possible to do it without angular.cli?

4
  • Have you seen the Angular testing guide here: angular.io/docs/ts/latest/testing Commented Apr 20, 2017 at 21:23
  • 1
    i am not looking for unit test. i am looking on how to call a javascript funciton from typescript Commented Apr 20, 2017 at 22:11
  • are u using angular-cli? u need to put this script to angular-cli.json file? Commented Apr 21, 2017 at 13:31
  • no, I don't use angular-cli. is there any solution without having to use angular cli? Commented Apr 21, 2017 at 21:39

3 Answers 3

4

Finally found a way to do it.

  1. in your html file that download the main.js file, put reference to the javascript file in the head tag enter image description here

  2. in your typescript file (in this case i am using angular) do the following:

a. declare the variable :

b. use the funciton in the constructor of the exported class

import { Component } from '@angular/core';
declare function testJs(): any;
@Component({
    selector: 'my-app',
    templateUrl: '/mainTemplate.html'
})

export class AppComponent {
    user: string;
    constructor() {
        testJs.prototype.testFunction();
        this.user = "asdf";
        var x = 90;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

After refresh i always get the undefined error. Do you know any fix?
declare function testJs(): any; in the above context, the testJs is not the filename. it is the function name. have you got that correct?
What is the "testFunction()" then?
just some random function. you can change it with whatever name
3
  1. where is get this testJS.js. Is this external lib? if yes, you need to include it in angular-cli like that:

    "scripts": [ "../node_modules/.../testJS.js" ],

  2. You need to either create typings or at least make dummy type:

    declare var testJS: any;

  3. You can use it afer

2 Comments

i don't think it's working... I just put a screenshot in my question above
create typings for a js file? how, please!
1

i'm use from eval()
1) before main.js file, put javascript file.
2) use from eval("testFunction()") in your typescript file in angular.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.