Home > Net >  How to access a variable from an external javascript from the Window interface in Angular (13.3.1)
How to access a variable from an external javascript from the Window interface in Angular (13.3.1)

Time:09-20

The external js file is imported in the app.module.ts file. This js file has a variable with some basic initialization and does some function on the Windows Interface based on the parameters set.

var jsVariable = {
  "init": function (params) {
        "use strict";
  jsVariable.parameters = params;
        if (alreadyLaunch === 0) {
            alreadyLaunch = 1;
            if (window.addEventListener) {

MyService is a ts component that must access this js variable and sets some of these parameters that that affects how the windows interface renders

@Injectable({
  providedIn: 'root',
})
export class MyService {  

  public jsVariable?:any;
   
  constructor() {
    if (typeof window !== 'undefined') {
 
      this.jsVariable= window.jsVariable;

      this.jsVariable.init({

I am however unable to access the jsVariable in my service component.

CodePudding user response:

this should work: this.jsVariable= (window as any).jsVariable

  • Related