Skip to content

Commit 9724247

Browse files
karamhevery
authored andcommitted
fix(ivy): generate empty QueryList for root component content queries (angular#29514)
In View Engine, we used to generate empty QueryLists for content queries on root components (though we did not actually support populating these lists). We need to keep this behavior in Ivy for backwards compatibility. Otherwise, components that are sometimes used as root will fail if they are relying on content query results to always be defined. PR Close angular#29514
1 parent 568140f commit 9724247

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/core/src/render3/component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ export function createRootComponent<T>(
217217
renderInitialStyles(native, rootTNode.stylingTemplate, componentView[RENDERER]);
218218
}
219219

220+
// We want to generate an empty QueryList for root content queries for backwards
221+
// compatibility with ViewEngine.
222+
if (componentDef.contentQueries) {
223+
componentDef.contentQueries(RenderFlags.Create, component, rootView.length - 1);
224+
}
225+
220226
return component;
221227
}
222228

packages/core/test/acceptance/query_spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ describe('query logic', () => {
401401

402402
});
403403

404+
// Some root components may have ContentChildren queries if they are also
405+
// usable as a child component. We should still generate an empty QueryList
406+
// for these queries when they are at root for backwards compatibility with
407+
// ViewEngine.
408+
it('should generate an empty QueryList for root components', () => {
409+
const fixture = TestBed.createComponent(QueryComp);
410+
fixture.detectChanges();
411+
expect(fixture.componentInstance.contentChildren).toBeAnInstanceOf(QueryList);
412+
expect(fixture.componentInstance.contentChildren.length).toBe(0);
413+
});
414+
404415
describe('descendants', () => {
405416

406417
it('should match directives on elements that used to be wrapped by a required parent in HTML parser',

0 commit comments

Comments
 (0)