fix(ivy): content query results should be available in content hooks (#25271)

PR Close #25271
This commit is contained in:
Kara Erickson 2018-08-02 14:27:36 -07:00
parent c2c12e52fe
commit 1fb7111da1
2 changed files with 25 additions and 4 deletions

View File

@ -301,12 +301,15 @@ function refreshDescendantViews() {
executeInitHooks(viewData, tView, creationMode);
}
refreshDynamicEmbeddedViews(viewData);
// Content query results must be refreshed before content hooks are called.
refreshContentQueries(tView);
if (!checkNoChangesMode) {
executeHooks(directives !, tView.contentHooks, tView.contentCheckHooks, creationMode);
}
setHostBindings(tView.hostBindings);
refreshContentQueries(tView);
refreshChildComponents(tView.components);
}

View File

@ -1708,12 +1708,19 @@ describe('query', () => {
describe('content', () => {
it('should support content queries for directives', () => {
let withContentInstance: WithContentDirective;
let withContentInstance: WithContentDirective|null = null;
class WithContentDirective {
// @ContentChildren('foo') foos;
// TODO(issue/24571): remove '!'.
foos !: QueryList<ElementRef>;
contentInitQuerySnapshot = 0;
contentCheckedQuerySnapshot = 0;
ngAfterContentInit() { this.contentInitQuerySnapshot = this.foos ? this.foos.length : 0; }
ngAfterContentChecked() {
this.contentCheckedQuerySnapshot = this.foos ? this.foos.length : 0;
}
static ngComponentDef = defineDirective({
type: WithContentDirective,
@ -1746,7 +1753,18 @@ describe('query', () => {
}, [WithContentDirective]);
const fixture = new ComponentFixture(AppComponent);
expect(withContentInstance !.foos.length).toBe(1);
expect(withContentInstance !.foos.length)
.toBe(1, `Expected content query to match <span #foo>.`);
expect(withContentInstance !.contentInitQuerySnapshot)
.toBe(
1,
`Expected content query results to be available when ngAfterContentInit was called.`);
expect(withContentInstance !.contentCheckedQuerySnapshot)
.toBe(
1,
`Expected content query results to be available when ngAfterContentChecked was called.`);
});
// https://stackblitz.com/edit/angular-wlenwd?file=src%2Fapp%2Fapp.component.ts