Angular2 - how to call component function from outside the app

Angular2 - how to call component function from outside the app

function callbackfunction(){   
   // window['angularComponentRef'] might not yet be set here though
   window['angularComponent'].zone.run(() => {
     runThisFunctionFromOutside(); 
   });
 }

constructor(private _ngZone: NgZone){
  window['angularComponentRef'] = {component: this, zone: _ngZone};
}

ngOnDestroy() {
  window.angularComponent = null;
}

use:

window['angularComponentRef'].zone.run(() => {window['angularComponentRef'].component.callFromOutside('1');})

OR

window.angularComponentRef.zone.run(() => {window.angularComponentRef.componentFn('2');})

Last updated

Was this helpful?