diff --git a/packages/core/src/zone/ng_zone.ts b/packages/core/src/zone/ng_zone.ts index 98b5a6aade..85130297c1 100644 --- a/packages/core/src/zone/ng_zone.ts +++ b/packages/core/src/zone/ng_zone.ts @@ -325,11 +325,17 @@ export class NoopNgZone implements NgZone { readonly onStable: EventEmitter = new EventEmitter(); readonly onError: EventEmitter = new EventEmitter(); - run(fn: () => any): any { return fn(); } + run(fn: (...args: any[]) => any, applyThis?: any, applyArgs?: any): any { + return fn.apply(applyThis, applyArgs); + } - runGuarded(fn: () => any): any { return fn(); } + runGuarded(fn: (...args: any[]) => any, applyThis?: any, applyArgs?: any): any { + return fn.apply(applyThis, applyArgs); + } - runOutsideAngular(fn: () => any): any { return fn(); } + runOutsideAngular(fn: (...args: any[]) => any): any { return fn(); } - runTask(fn: () => any): any { return fn(); } + runTask(fn: (...args: any[]) => any, applyThis?: any, applyArgs?: any, name?: string): any { + return fn.apply(applyThis, applyArgs); + } } diff --git a/packages/core/test/zone/ng_zone_spec.ts b/packages/core/test/zone/ng_zone_spec.ts index 5ed7c61ea9..ba8e795727 100644 --- a/packages/core/test/zone/ng_zone_spec.ts +++ b/packages/core/test/zone/ng_zone_spec.ts @@ -182,6 +182,36 @@ function runNgZoneNoLog(fn: () => any) { expect(runs).toBe(true); }); + it('should run with this context and arguments', () => { + let runs = false; + let applyThisArray: any[] = []; + let applyArgsArray: any[] = []; + const testContext = {}; + const testArgs = ['args']; + ngZone.run(function(this: any, arg: any) { + applyThisArray.push(this); + applyArgsArray.push([arg]); + ngZone.runGuarded(function(this: any, argGuarded: any) { + applyThisArray.push(this); + applyArgsArray.push([argGuarded]); + ngZone.runOutsideAngular(function(this: any, argOutsideAngular: any) { + applyThisArray.push(this); + applyArgsArray.push([argOutsideAngular]); + runs = true; + }); + }, this, [arg]); + }, testContext, testArgs); + expect(runs).toBe(true); + expect(applyThisArray.length).toBe(3); + expect(applyArgsArray.length).toBe(3); + expect(applyThisArray[0]).toBe(testContext); + expect(applyThisArray[1]).toBe(testContext); + expect(applyThisArray[2]).not.toBe(testContext); + expect(applyArgsArray[0]).toEqual(testArgs); + expect(applyArgsArray[1]).toEqual(testArgs); + expect(applyArgsArray[2]).toEqual([undefined]); + }); + it('should have EventEmitter instances', () => { expect(ngZone.onError instanceof EventEmitter).toBe(true); expect(ngZone.onStable instanceof EventEmitter).toBe(true);