From 409860a4dad1596fec25f04f4d895cd2abb19bb8 Mon Sep 17 00:00:00 2001 From: Kara Erickson Date: Thu, 9 Aug 2018 14:07:47 -0700 Subject: [PATCH] fix(ivy): queries should be restored when view changes (#25415) PR Close #25415 --- packages/core/src/render3/instructions.ts | 1 + .../hello_world/bundle.golden_symbols.json | 3 ++ packages/core/test/render3/query_spec.ts | 48 ++++++++++++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/packages/core/src/render3/instructions.ts b/packages/core/src/render3/instructions.ts index 26b60c12d3..f44d5d8bea 100644 --- a/packages/core/src/render3/instructions.ts +++ b/packages/core/src/render3/instructions.ts @@ -274,6 +274,7 @@ export function enterView(newView: LViewData, host: LElementNode | LViewNode | n } viewData = contextViewData = newView; + oldView && (oldView[QUERIES] = currentQueries); currentQueries = newView && newView[QUERIES]; return oldView; diff --git a/packages/core/test/bundling/hello_world/bundle.golden_symbols.json b/packages/core/test/bundling/hello_world/bundle.golden_symbols.json index f2a8f4b7b0..5e2a1f5010 100644 --- a/packages/core/test/bundling/hello_world/bundle.golden_symbols.json +++ b/packages/core/test/bundling/hello_world/bundle.golden_symbols.json @@ -62,6 +62,9 @@ { "name": "PublicFeature" }, + { + "name": "QUERIES" + }, { "name": "RENDERER" }, diff --git a/packages/core/test/render3/query_spec.ts b/packages/core/test/render3/query_spec.ts index 58854ac4e6..4012a7aaca 100644 --- a/packages/core/test/render3/query_spec.ts +++ b/packages/core/test/render3/query_spec.ts @@ -11,7 +11,7 @@ import {ElementRef, TemplateRef, ViewContainerRef} from '@angular/core'; import {EventEmitter} from '../..'; import {QUERY_READ_CONTAINER_REF, QUERY_READ_ELEMENT_REF, QUERY_READ_FROM_NODE, QUERY_READ_TEMPLATE_REF, getOrCreateNodeInjectorForNode, getOrCreateTemplateRef} from '../../src/render3/di'; -import {AttributeMarker, QueryList, defineComponent, defineDirective, detectChanges, injectViewContainerRef} from '../../src/render3/index'; +import {AttributeMarker, QueryList, defineComponent, defineDirective, detectChanges, injectTemplateRef, injectViewContainerRef} from '../../src/render3/index'; import {bind, container, containerRefreshEnd, containerRefreshStart, element, elementContainerEnd, elementContainerStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, load, loadDirective, loadElement, loadQueryList, registerContentQuery} from '../../src/render3/instructions'; import {RenderFlags} from '../../src/render3/interfaces/definition'; import {query, queryRefresh} from '../../src/render3/query'; @@ -1780,6 +1780,52 @@ describe('query', () => { }); }); + it('should restore queries if view changes', () => { + class SomeDir { + constructor(public vcr: ViewContainerRef, public temp: TemplateRef) { + this.vcr.createEmbeddedView(this.temp); + } + + static ngDirectiveDef = defineDirective({ + type: SomeDir, + selectors: [['', 'someDir', '']], + factory: () => new SomeDir(injectViewContainerRef(), injectTemplateRef()) + }); + } + + function template(rf: RenderFlags, ctx: any) { + if (rf & RenderFlags.Create) { + element(0, 'div'); + } + } + + /** + *
+ *
+ */ + const AppComponent = createComponent( + 'app', + function(rf: RenderFlags, ctx: any) { + if (rf & RenderFlags.Create) { + container(1, template, null, [AttributeMarker.SelectOnly, 'someDir']); + element(2, 'div', null, ['foo', '']); + } + }, + [SomeDir], [], + function(rf: RenderFlags, ctx: any) { + if (rf & RenderFlags.Create) { + query(0, ['foo'], true, QUERY_READ_FROM_NODE); + } + if (rf & RenderFlags.Update) { + let tmp: any; + queryRefresh(tmp = load>(0)) && (ctx.query = tmp as QueryList); + } + }); + + const fixture = new ComponentFixture(AppComponent); + expect(fixture.component.query.length).toBe(1); + }); + describe('content', () => { let withContentInstance: WithContentDirective|null; let shallowCompInstance: ShallowComp|null;