Reputation: 1245
I have an angular 6 app which will be embed inside another application. That application injects javascript to the browser in order to expose certain functionalities to the angular app.
As an example, when i run my angular app inside the mentioned application, if i go to the web console, i can execute the following js method
API.callService("Special Service");
and works fine.
How can i execute the same inside the angular app ?
i tried the following in the component
declare var API: any;
and then at ngAfterViewInit() method
API.callService("Special Service");
but i get an error as API is undefined.
As extra information, i can use the chrome dev tools for the embed browser. If i want to run the javascript method i just do..
And that works fine ! The issue is to do the same call from the Angular component.
Thanks!
Upvotes: 0
Views: 506
Reputation: 1245
All,
I found the issue
Despite of the fact i was calling the api in the ngAfterViewInit, it seems that the api was not available.
i tried with
setTimeout(()=>this.click(), 1000);
click(){
var result = SrwOSGiApi.callOSGIService("com.sabre.edge.platform.core.sso.base.IAgentProfileService","getPcc", null);
}
and works fine !!
Thanks!!
Upvotes: 0
Reputation: 319
I hope this help you
declare var API: any;
You are saying API as any value, so you get undefined;
Try on your constructor or ngAfterViewInit
this.API = API.callService("Special Service");
Upvotes: 1
Reputation: 696
You need to export your javascript functions, then you can require them in your angular module.
Of course, you'll need to be able or have the right to modify the original javascript file to add the module exports declarations.
To use the 'require' function in an angular component, you'll need to run :
$ npm install @types/node
Then in tsconfig.json, under the "typeRoots" property, add :
"types": ["node"],
Here is a quick example here: https://stackblitz.com/edit/angular-qphccu
Upvotes: 0