Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ivy): support checkNoChanges on embedded views #28644

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/core/src/render3/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2751,7 +2751,7 @@ function tickRootContext(rootContext: RootContext) {
* @param component The component which the change detection should be performed on.
*/
export function detectChanges<T>(component: T): void {
const view = getComponentViewByInstance(component) !;
const view = getComponentViewByInstance(component);
detectChangesInternal<T>(view, component);
}

Expand Down Expand Up @@ -2790,9 +2790,14 @@ export function detectChangesInRootView(lView: LView): void {
* introduce other changes.
*/
export function checkNoChanges<T>(component: T): void {
const view = getComponentViewByInstance(component);
checkNoChangesInternal<T>(view, component);
}

export function checkNoChangesInternal<T>(view: LView, context: T) {
setCheckNoChangesMode(true);
try {
detectChanges(component);
detectChangesInternal(view, context);
} finally {
setCheckNoChangesMode(false);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/render3/view_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import {ChangeDetectorRef as viewEngine_ChangeDetectorRef} from '../change_detec
import {ViewContainerRef as viewEngine_ViewContainerRef} from '../linker/view_container_ref';
import {EmbeddedViewRef as viewEngine_EmbeddedViewRef, InternalViewRef as viewEngine_InternalViewRef} from '../linker/view_ref';

import {checkNoChanges, checkNoChangesInRootView, checkView, detectChangesInRootView, detectChangesInternal, markViewDirty, storeCleanupFn, viewAttached} from './instructions';
import {checkNoChangesInRootView, checkNoChangesInternal, detectChangesInRootView, detectChangesInternal, markViewDirty, storeCleanupFn} from './instructions';
import {TNode, TNodeType, TViewNode} from './interfaces/node';
import {FLAGS, HOST, LView, LViewFlags, PARENT, RENDERER_FACTORY, T_HOST} from './interfaces/view';
import {FLAGS, HOST, LView, LViewFlags, PARENT, T_HOST} from './interfaces/view';
import {destroyLView} from './node_manipulation';
import {getNativeByTNode} from './util';

Expand Down Expand Up @@ -252,7 +252,7 @@ export class ViewRef<T> implements viewEngine_EmbeddedViewRef<T>, viewEngine_Int
* This is used in development mode to verify that running change detection doesn't
* introduce other changes.
*/
checkNoChanges(): void { checkNoChanges(this.context); }
checkNoChanges(): void { checkNoChangesInternal(this._lView, this.context); }

attachToViewContainerRef(vcRef: viewEngine_ViewContainerRef) {
if (this._appRef) {
Expand Down
71 changes: 71 additions & 0 deletions packages/core/test/acceptance/change_detection_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/


import {ApplicationRef, Component, Directive, EmbeddedViewRef, TemplateRef, ViewContainerRef} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/src/matchers';

describe('change detection', () => {

describe('embedded views', () => {

@Directive({selector: '[viewManipulation]', exportAs: 'vm'})
class ViewManipulation {
constructor(
private _tplRef: TemplateRef<{}>, private _vcRef: ViewContainerRef,
private _appRef: ApplicationRef) {}

insertIntoVcRef() { this._vcRef.createEmbeddedView(this._tplRef); }

insertIntoAppRef(): EmbeddedViewRef<{}> {
const viewRef = this._tplRef.createEmbeddedView({});
this._appRef.attachView(viewRef);
return viewRef;
}
}

@Component({
selector: 'test-cmp',
template: `
<ng-template #vm="vm" viewManipulation>{{'change-detected'}}</ng-template>
`
})
class TestCmpt {
}

beforeEach(() => {
TestBed.configureTestingModule({declarations: [TestCmpt, ViewManipulation]});
});

it('should detect changes for embedded views inserted through ViewContainerRef', () => {
const fixture = TestBed.createComponent(TestCmpt);
const vm = fixture.debugElement.childNodes[0].references['vm'] as ViewManipulation;

vm.insertIntoVcRef();
fixture.detectChanges();

expect(fixture.nativeElement).toHaveText('change-detected');
});

it('should detect changes for embedded views attached to ApplicationRef', () => {
const fixture = TestBed.createComponent(TestCmpt);
const vm = fixture.debugElement.childNodes[0].references['vm'] as ViewManipulation;

const viewRef = vm.insertIntoAppRef();

// A newly created view was attached to the CD tree via ApplicationRef so should be also
// change detected when ticking root component
fixture.detectChanges();

expect(viewRef.rootNodes[0]).toHaveText('change-detected');
});

});

});
5 changes: 1 addition & 4 deletions packages/core/test/bundling/todo/bundle.golden_symbols.json
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@
"name": "callHooks"
},
{
"name": "checkNoChanges"
"name": "checkNoChangesInternal"
},
{
"name": "checkNoChangesMode"
Expand Down Expand Up @@ -527,9 +527,6 @@
{
"name": "detachView"
},
{
"name": "detectChanges"
},
{
"name": "detectChangesInternal"
},
Expand Down