1

I have coded a js file to return some values to ts files, in my angular project.

var webGlObject = (function() { 
  return { 
    init: function() { 
      alert('webGlObject initialized');
    } 
  } 
})(webGlObject||{})
import { Component, OnInit } from '@angular/core';
import '../../../lib/jquery-3.2.1.min.js';
import '../../../server/getSummary.js';

declare var webGlObject: any;

@Component({
  selector: 'mop-designer-dashboard',
  templateUrl: 'mop-designer-dashboard.component.html',
  styleUrls: ['mop-designer-dashboard.component.css'],
})

export class MopDesignerDashboardComponent implements OnInit {

  debugger;
  scoreBoardList: any = [];
  breadcrumb: any = [];

  constructor() {

    /* prepare breadcrumb */
    this.breadcrumb = [
      {
        label: 'Mop Designer',
        href: '/#/mop-designer'
      }
    ];

    debugger;

    webGlObject.init();



  }

  ngOnInit() {
    console.log('test');
  }
}

but declare var webGlObject: any; doesn't create any object and I get following error:

> ZoneTask.invoke @ zone.js?fad3:339 VM292602:61 Error: Uncaught (in promise): Error: Error in :0:0 caused by: webGlObject is not defined ReferenceError: webGlObject is not defined at new MopDesignerDashboardComponent

1 Answer 1

2

This is because you are creating it as a class and using as an object. Try this: Put following in js file:

var webGlObject = function() {
this.init= function() {
  alert('webGlObject initialized');}}

And create an instance in ts file:

declare var webGlObject: any;
export class YourComponent{
constructor(){
    let wegObj = new webGlObject();
    wegObj.init();
  }
}
Sign up to request clarification or add additional context in comments.

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.