Angular 2 typescript invoke javascript function

Joshua

Is there a correct way to invoke a JavaScript function from a component in Angular 2 (TypeScript) ?

Here is my component :

import { ElementRef, AfterViewInit }       from '@angular/core';

export class AppComponent implements AfterViewInit {

    constructor(private _elementRef: ElementRef) {
    }

    ngAfterViewInit() {
        /**
         * Works but i have this error :
         * src/app.component.ts(68,9): error TS2304: Cannot find name 'MYTHEME'.
         * src/app.component.ts(69,9): error TS2304: Cannot find name 'MYTHEME'.
         */
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();

        /**
         * Works without error, but doesn't seem like a right way to do it
         */
        var s = document.createElement("script");
        s.text = "MYTHEME.documentOnLoad.init(); MYTHEME.documentOnReady.init();";
        this._elementRef.nativeElement.appendChild(s);
    }
}

Calling the JavaScript function directly result in an compilation error, but the syntax in the "compiled" JavaScript file (app.component.js) is correct :

AppComponent.prototype.ngAfterViewInit = function () {
    MYTHEME.documentOnLoad.init();
    MYTHEME.documentOnReady.init();
};

The 2nd way (appendChild) works without error, but i don't think (altering the DOM from typescript/angular) is the correct way to do it.

I found this : Using a Javascript Function from Typescript I tried declaring the interface :

interface MYTHEME {
    documentOnLoad: Function;
    documentOnReady: Function;
}

But the TypeScript doesn't seem to recognize it (no error in the interface declaration).

Thanks

Edit :

Following the answer by Juan Mendes this is what i ended with :

import { AfterViewInit }       from '@angular/core';

interface MYTHEME {
    documentOnLoad: INIT;
    documentOnReady: INIT;
}
interface INIT {
    init: Function;
}
declare var MYTHEME: MYTHEME;

export class AppComponent implements AfterViewInit {

    constructor() {
    }

    ngAfterViewInit() {
        MYTHEME.documentOnLoad.init(); 
        MYTHEME.documentOnReady.init();
    }
}
Juan Mendes

You have to tell TypeScript about external (JavaScript) declarations using declare. See https://www.typescriptlang.org/docs/handbook/writing-declaration-files.html

interface MyTheme {
    documentOnLoad: Function;
    documentOnReady: Function;
}
declare var MYTHEME: MyTheme;

Or anonymously

declare var MYTHEME: {documentOnLoad: Function, documentOnReady: Function};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How typescript is converted to javascript in angular2?

From Dev

Passing function as JSON data in angular 2 typescript

From Dev

Angular 2 Typescript: Is it possible to pass an interface as a parameter into a function?

From Dev

TypeScript/Angular 2 - call a function after another completes

From Dev

Angular 2 - How to call a typescript function inside of an addEventListener handler function?

From Dev

invoke pipe when passing function as arg Pipe angular2

From Dev

How to invoke a javascript function (generated from typescript) trapped within "System.register()" module while using Google protobuf?

From Dev

Angular 2 table invoke connect function

From Dev

Invoke a function multiple times with the same argument JavaScript

From Dev

It's possible to invoke a new native function with COCOS2D javascript bindings

From Dev

Invoke a javascript function of a website from java code?

From Dev

Invoke a Function with the Correct Parameters from an Object in JavaScript

From Dev

Angular 2 + typescript or Angular 2 + Javascript which one is advisable

From Dev

.every function in typescript + angular 2

From Dev

Invoke Angular2 Component function from VisJS Event

From Dev

Nested function in Angular2 using TypeScript

From Dev

angular2 typescript $.cookie() is not a function

From Dev

Calling Typescript function from html2canvas in angular 2

From Dev

Javascript : use string to invoke function

From Dev

Angular 2 + Typescript displaying variable from within function

From Dev

How to call an external javascript library function within a Typescript class using Angular 2/4?

From Dev

Angular/Typescript function(){} vs ()=>{}

From Dev

Typescript, function with 2 types of params and returns: Cannot invoke an expression whose type lacks a call signature

From Dev

Invoke Javascript function from Angular

From Dev

invoke angular service call inside "then(function(){})"

From Dev

Unable to invoke javascript function from blazor component

From Dev

How to invoke javascript function when # is present in URL

From Dev

How can I use a JavaScript function in a Angular TypeScript component?

From Dev

Access TypeScript function from Javascript callback Angular

Related Related

  1. 1

    How typescript is converted to javascript in angular2?

  2. 2

    Passing function as JSON data in angular 2 typescript

  3. 3

    Angular 2 Typescript: Is it possible to pass an interface as a parameter into a function?

  4. 4

    TypeScript/Angular 2 - call a function after another completes

  5. 5

    Angular 2 - How to call a typescript function inside of an addEventListener handler function?

  6. 6

    invoke pipe when passing function as arg Pipe angular2

  7. 7

    How to invoke a javascript function (generated from typescript) trapped within "System.register()" module while using Google protobuf?

  8. 8

    Angular 2 table invoke connect function

  9. 9

    Invoke a function multiple times with the same argument JavaScript

  10. 10

    It's possible to invoke a new native function with COCOS2D javascript bindings

  11. 11

    Invoke a javascript function of a website from java code?

  12. 12

    Invoke a Function with the Correct Parameters from an Object in JavaScript

  13. 13

    Angular 2 + typescript or Angular 2 + Javascript which one is advisable

  14. 14

    .every function in typescript + angular 2

  15. 15

    Invoke Angular2 Component function from VisJS Event

  16. 16

    Nested function in Angular2 using TypeScript

  17. 17

    angular2 typescript $.cookie() is not a function

  18. 18

    Calling Typescript function from html2canvas in angular 2

  19. 19

    Javascript : use string to invoke function

  20. 20

    Angular 2 + Typescript displaying variable from within function

  21. 21

    How to call an external javascript library function within a Typescript class using Angular 2/4?

  22. 22

    Angular/Typescript function(){} vs ()=>{}

  23. 23

    Typescript, function with 2 types of params and returns: Cannot invoke an expression whose type lacks a call signature

  24. 24

    Invoke Javascript function from Angular

  25. 25

    invoke angular service call inside "then(function(){})"

  26. 26

    Unable to invoke javascript function from blazor component

  27. 27

    How to invoke javascript function when # is present in URL

  28. 28

    How can I use a JavaScript function in a Angular TypeScript component?

  29. 29

    Access TypeScript function from Javascript callback Angular

HotTag

Archive