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): refresh child components before executing ViewQuery function #32922

Closed
wants to merge 3 commits into from
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
13 changes: 8 additions & 5 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,17 +430,20 @@ export function refreshView<T>(

setHostBindings(tView, lView);

const viewQuery = tView.viewQuery;
if (viewQuery !== null) {
executeViewQueryFn(RenderFlags.Update, viewQuery, context);
}

// Refresh child component views.
const components = tView.components;
if (components !== null) {
refreshChildComponents(lView, components);
}

// View queries must execute after refreshing child components because a template in this view
// could be inserted in a child component. If the view query executes before child component
// refresh, the template might not yet be inserted.
const viewQuery = tView.viewQuery;
atscott marked this conversation as resolved.
Show resolved Hide resolved
if (viewQuery !== null) {
executeViewQueryFn(RenderFlags.Update, viewQuery, context);
}

// execute view hooks (AfterViewInit, AfterViewChecked)
// PERF WARNING: do NOT extract this to a separate function without running benchmarks
if (!checkNoChangesMode) {
Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/acceptance/query_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,42 @@ describe('query logic', () => {
expect(fixture.componentInstance.foo.length).toBe(2);
});

it('should support ViewChild query where template is inserted in child component', () => {
@Component({selector: 'required', template: ''})
class Required {
}

@Component({
selector: 'insertion',
template: `<ng-container [ngTemplateOutlet]="content"></ng-container>`
})
class Insertion {
@Input() content !: TemplateRef<{}>;
}

@Component({
template: `
<ng-template #template>
<required></required>
</ng-template>
<insertion [content]="template"></insertion>
`
})
class App {
@ViewChild(Required, {static: false}) requiredEl !: Required;
viewChildAvailableInAfterViewInit?: boolean;

ngAfterViewInit() {
this.viewChildAvailableInAfterViewInit = this.requiredEl !== undefined;
}
}

const fixture = TestBed.configureTestingModule({declarations: [App, Insertion, Required]})
.createComponent(App);
fixture.detectChanges();
expect(fixture.componentInstance.viewChildAvailableInAfterViewInit).toBe(true);
});

});

describe('content queries', () => {
Expand Down