0

I am trying to return value from callback function in angular2 service and using that value (return self.globalMenu) in my component but the callback function is not executing and it is throwing "callback function is undefined" error .

It worked properly in Angular 1 and after migrating the code to Angular 2,It stopped working .

Could someone help me out in fixing this error and returning value from callback function in service ?

portalmode.service.ts :

import { Injectable,Inject } from '@angular/core';
@Injectable()
export class MenuViewModelProvider {
  globalMenu: any;

  constructor(@Inject('DcsPlus.Frame.Logic.portalComponentManager') private portalComponentManager,
              @Inject('DcsPlus.Frame.Logic.viewModelProvider') private viewModelProvider) {}

  public setMenuCommandItems(mmCommandItems,appId): any {
    let self = this;
    self.globalMenu = self.getMenu(mmCommandItems);
      this.viewModelProvider.register("mmMenu", function () {
          return self.globalMenu;
      });
  }
}

menu.component.ts :

export class MainMenuComponent implements OnInit{

    constructor(@Inject('DcsPlus.Frame.Logic.viewModelProvider') private viewModelProvider) {}

  mainMenu:any=this;
  viewModel:any;

    ngOnInit() {
        this.viewModelProvider.load('mmMenu').then(function () {
            let menu = this.viewModelProvider.get('mmMenu');
            this.mainMenu.items = menu.items;
        });
    }
}
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
Vivek
  • 281
  • 2
  • 7
  • 21
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Aluan Haddad Feb 03 '18 at 05:00
  • This is not an Angular question it is about asynchronous programming in JavaScript. Please see the linked question and its very thorough accepted answer to learn the concepts. – Aluan Haddad Feb 03 '18 at 05:05
  • @AluanHaddad I see a lot of responses to this issue in the possible duplicate link, but I see most of the solutions to handle the callback response continue in the javascript code, what about when this is Angular 2 and the result of the callback is needed by the html template? in the question above Vivek needs the callback result probably for the html just display the menu items. – Chriso Lopez Dec 21 '21 at 15:31

0 Answers0