diff --git a/modules/@angular/core/src/linker/view_ref.ts b/modules/@angular/core/src/linker/view_ref.ts index 1f54813126..1f24f4b170 100644 --- a/modules/@angular/core/src/linker/view_ref.ts +++ b/modules/@angular/core/src/linker/view_ref.ts @@ -80,7 +80,13 @@ export abstract class EmbeddedViewRef extends ViewRef { } export class ViewRef_ implements EmbeddedViewRef, ChangeDetectorRef { - constructor(private _view: AppView) { this._view = _view; } + /** @internal */ + _originalMode: ChangeDetectionStrategy; + + constructor(private _view: AppView) { + this._view = _view; + this._originalMode = this._view.cdMode; + } get internalView(): AppView { return this._view; } @@ -95,7 +101,7 @@ export class ViewRef_ implements EmbeddedViewRef, ChangeDetectorRef { detectChanges(): void { this._view.detectChanges(false); } checkNoChanges(): void { this._view.detectChanges(true); } reattach(): void { - this._view.cdMode = ChangeDetectionStrategy.CheckAlways; + this._view.cdMode = this._originalMode; this.markForCheck(); } diff --git a/modules/@angular/core/test/linker/change_detection_integration_spec.ts b/modules/@angular/core/test/linker/change_detection_integration_spec.ts index cd4b8d965d..a54fe5d102 100644 --- a/modules/@angular/core/test/linker/change_detection_integration_spec.ts +++ b/modules/@angular/core/test/linker/change_detection_integration_spec.ts @@ -1032,6 +1032,43 @@ export function main() { expect(renderLog.log).toEqual([]); })); + + it('Reattaches', fakeAsync(() => { + var ctx = createCompFixture(''); + var cmp: CompWithRef = queryDirs(ctx.debugElement, CompWithRef)[0]; + + cmp.value = 'hello'; + cmp.changeDetectorRef.detach(); + + ctx.detectChanges(); + + expect(renderLog.log).toEqual([]); + + cmp.changeDetectorRef.reattach(); + + ctx.detectChanges(); + + expect(renderLog.log).toEqual(['{{hello}}']); + + })); + + it('Reattaches in the original cd mode', fakeAsync(() => { + var ctx = createCompFixture(''); + var cmp: PushComp = queryDirs(ctx.debugElement, PushComp)[0]; + cmp.changeDetectorRef.detach(); + cmp.changeDetectorRef.reattach(); + + // renderCount should NOT be incremented with each CD as CD mode should be resetted to + // on-push + ctx.detectChanges(); + expect(cmp.renderCount).toBeGreaterThan(0); + var count = cmp.renderCount; + + ctx.detectChanges(); + expect(cmp.renderCount).toBe(count); + + })); + }); describe('multi directive order', () => { @@ -1192,7 +1229,7 @@ class CompWithRef { @Component({ selector: 'push-cmp', - template: '
{{value}}', + template: '
{{value}}{{renderIncrement}}', host: {'(event)': 'noop()'}, directives: ALL_DIRECTIVES, pipes: ALL_PIPES, @@ -1200,6 +1237,12 @@ class CompWithRef { }) class PushComp { @Input() public value: any; + public renderCount: any = 0; + + get renderIncrement() { + this.renderCount++; + return ''; + } constructor(public changeDetectorRef: ChangeDetectorRef) {}