Skip to content

Commit d58dea0

Browse files
committedSep 3, 2018
fix(elements): initialization should run in ngZone (angular#24181)
1 parent 80a9f46 commit d58dea0

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed
 

‎packages/elements/src/component-factory-strategy-zone.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
import {ComponentFactory, ComponentRef, Injector, NgZone} from '@angular/core';
10+
import {} from 'zone.js';
1011

1112
import {ComponentNgElementStrategy, ComponentNgElementStrategyFactory} from './component-factory-strategy';
1213

@@ -28,10 +29,12 @@ export class ComponentNgElementZoneStrategyFactory extends ComponentNgElementStr
2829
*/
2930
export class ComponentNgElementZoneStrategy extends ComponentNgElementStrategy {
3031
private readonly ngZone: NgZone;
32+
private elementZone: Zone;
3133

3234
constructor(componentFactory: ComponentFactory<any>, injector: Injector) {
3335
super(componentFactory, injector);
3436
this.ngZone = this.injector.get<NgZone>(NgZone);
37+
this.ngZone.run(() => { this.elementZone = Zone.current; });
3538
}
3639

3740
connect(element: HTMLElement) {
@@ -50,5 +53,11 @@ export class ComponentNgElementZoneStrategy extends ComponentNgElementStrategy {
5053
this.runInZone(() => { super.setInputValue(propName, value); });
5154
}
5255

53-
private runInZone(fn: () => any) { return NgZone.isInAngularZone() ? fn() : this.ngZone.run(fn); }
54-
}
56+
/**
57+
* Multiple elements may have their own NgZone and NgZone.isInAngularZone() doesn't
58+
* distinguish between them
59+
*/
60+
private runInZone(fn: () => any) {
61+
return (Zone.current === this.elementZone) ? fn() : this.ngZone.run(fn);
62+
}
63+
}

‎packages/elements/test/component-factory-strategy_zone_spec.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ describe('ComponentNgElementZoneStrategyFactory', () => {
5656

5757
it('should connect already in NgZone', () => {
5858
const parent = spyOn(ComponentNgElementStrategy.prototype, 'connect');
59-
const isInAngularZone =
60-
spyOn(NgZone, 'isInAngularZone').and.callFake(function() { return true; });
59+
strategy['elementZone'] = Zone.current;
6160
ngZone.run.calls.reset();
6261
strategy.connect(document.createElement('div'));
6362
expect(parent).toHaveBeenCalled();
@@ -66,8 +65,7 @@ describe('ComponentNgElementZoneStrategyFactory', () => {
6665

6766
it('should connect not already in NgZone', () => {
6867
const parent = spyOn(ComponentNgElementStrategy.prototype, 'connect');
69-
const isInAngularZone =
70-
spyOn(NgZone, 'isInAngularZone').and.callFake(function() { return false; });
68+
strategy['elementZone'] = Zone.current.parent;
7169
ngZone.run.calls.reset();
7270
strategy.connect(document.createElement('div'));
7371
expect(parent).toHaveBeenCalled();
@@ -76,8 +74,7 @@ describe('ComponentNgElementZoneStrategyFactory', () => {
7674

7775
describe('after connected not in NgZone', () => {
7876
beforeEach(() => {
79-
const isInAngularZone =
80-
spyOn(NgZone, 'isInAngularZone').and.callFake(function() { return false; });
77+
strategy['elementZone'] = Zone.current.parent;
8178
strategy.connect(document.createElement('div'));
8279
ngZone.run.calls.reset();
8380
});

0 commit comments

Comments
 (0)
Please sign in to comment.