0

Can anyone help me how to call a function from external js file inside my angular2 component. Below is my code snippet, when I am trying to load the application it is throwing App is undefined error.

Here is my component code:

import { Component, OnInit, AfterContentInit } from '@angular/core';

@Component({
    selector: 'test',
    templateUrl: './test.html'
})

export class TestComponent implements OnInit {

    constructor() { }

    ngOnInit() {

    }

    ngAfterViewInit() {
        App.handleInit();
    }
}

here is my script inside app.js:

var App = function() {
    var handleInit = function() {
        console.log("Hello world");
    };
}();

When we are calling a function inside ngAfterViewInit it is throwing App is undefined error.

We are importing app.js file inside vendor.ts and we are using webpack. Here app.js is loading fine but function was not working.

Please help us with a solution.

Thanks in advance.

4
  • there is no angularjs 2, is just angular Commented Jun 13, 2017 at 11:54
  • We mean it is Angular2 Commented Jun 13, 2017 at 12:06
  • create an interface for you js file and load that in you app module and access that method inside your angular 2 component Commented Jun 13, 2017 at 12:27
  • Can you please provide an example for that. Commented Jun 13, 2017 at 12:36

2 Answers 2

0

You have to just simply define App variable in your component as below.

declare var App: any;
@Component({
   .....
})

And now your below call will work.

ngAfterViewInit() {
    App.handleInit();
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi Sandip, Thanks for your response, We tried the above but still getting App is undefined error.
Is your JS is available in DOM? because I have tried this type of code in several component and it works for me
Yes the file is loading but function is not calling. We are loading the js files in vendor.ts but not in index.html file.
It seems when you call your function JS file and within function is not reachable, you have to import OR link that JS to global OR in required comonent
You can load that javascript in your comoponent as well look at here: stackoverflow.com/a/44276683/6606630
|
0

You can access like that. does this help

import { Component, OnInit, AfterContentInit } from '@angular/core';
import '../../../../js/app.js'; //just example set this based on your path
declare var App: any;

@Component({
    selector: 'test',
    templateUrl: './test.html'
})
export class TestComponent implements OnInit {

    constructor() { }

    ngOnInit() { }

    ngAfterViewInit() {
        this.handleInit();
    }

    hadeleInit() {
        new app.handleInit();
    }
}

7 Comments

Hi Shailesh Ladumor, Thanks for your quick response, but here the problem is there is no export member in our app.js file(like App), so we are unable to import like what you suggested. Is there any other approach?
where you add this js file?
i have updated my answer. you need to import app.js file
Still we are getting App is not defined error like below. Uncaught (in promise): Error: Error in :0:0 caused by: App is not defined
|

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.