angular - Angular2 EventEmitter doesn't raise event when emitted in a callback -


i'm developing angular2 component uses webcam snap picture canvas element, , extract blob. seperately, blob , event work correctly, when try emit event within toblob's callback function, event no longer raised listeners.

this works:

var self = this;  self.gotsnapshot.emit({}); //raises gotsnapshot event correctly.  this.canvas.toblob(function(blob){   console.log(blob); //logs blob console correctly. }); 

this doesn't:

var self = this;  this.canvas.toblob(function(blob){   self.gotsnapshot.emit(blob); //event never raised listeners }); 

i'm sure it's because toblob executing asynchronously. in angular1 instinct $timeout , call apply, thought eventemitter don't have deal anymore?

anyone know how around this?

this api not patched angulars zone. need explicitly make code run in angulars zone change detection happen afterwards:

constructor(private zone:ngzone){}  somemethod() {   this.canvas.toblob((blob) => {     this.zone.run(() => {       this.gotsnapshot.emit(blob);      });   }); } 

you don't need self if use () => instead of function()


Comments