diff --git a/packages/zone.js/lib/node/events.ts b/packages/zone.js/lib/node/events.ts index 2366332c5c..f09f8a4216 100644 --- a/packages/zone.js/lib/node/events.ts +++ b/packages/zone.js/lib/node/events.ts @@ -16,6 +16,7 @@ Zone.__load_patch('EventEmitter', (global: any) => { const EE_REMOVE_ALL_LISTENER = 'removeAllListeners'; const EE_LISTENERS = 'listeners'; const EE_ON = 'on'; + const EE_OFF = 'off'; const compareTaskCallbackVsDelegate = function(task: any, delegate: any) { // same callback, same capture, same event name, just return @@ -47,6 +48,7 @@ Zone.__load_patch('EventEmitter', (global: any) => { }); if (result && result[0]) { obj[EE_ON] = obj[EE_ADD_LISTENER]; + obj[EE_OFF] = obj[EE_REMOVE_LISTENER]; } } diff --git a/packages/zone.js/test/node/events.spec.ts b/packages/zone.js/test/node/events.spec.ts index f50b0eb02f..9cab424169 100644 --- a/packages/zone.js/test/node/events.spec.ts +++ b/packages/zone.js/test/node/events.spec.ts @@ -66,6 +66,18 @@ describe('nodejs EventEmitter', () => { emitter.emit('test2', 'test value'); }); }); + it('should remove listeners by calling off properly', () => { + zoneA.run(() => { + emitter.on('test', shouldNotRun); + emitter.on('test2', shouldNotRun); + emitter.off('test', shouldNotRun); + }); + zoneB.run(() => { + emitter.off('test2', shouldNotRun); + emitter.emit('test', 'test value'); + emitter.emit('test2', 'test value'); + }); + }); it('remove listener should return event emitter', () => { zoneA.run(() => { emitter.on('test', shouldNotRun);