diff --git a/modules/@angular/compiler/src/view_compiler/compile_element.ts b/modules/@angular/compiler/src/view_compiler/compile_element.ts index ce7d9a88800f9..a29dd540feed6 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_element.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_element.ts @@ -210,12 +210,7 @@ export class CompileElement extends CompileNode { const directiveInstance = this.instances.get(tokenReference(identifierToken(directive.type))); directive.queries.forEach((queryMeta) => { this._addQuery(queryMeta, directiveInstance); }); } - const queriesWithReads: _QueryWithRead[] = []; - Array.from(this._resolvedProviders.values()).forEach((resolvedProvider) => { - const queriesForProvider = this._getQueriesFor(resolvedProvider.token); - queriesWithReads.push( - ...queriesForProvider.map(query => new _QueryWithRead(query, resolvedProvider.token))); - }); + Object.keys(this.referenceTokens).forEach(varName => { const token = this.referenceTokens[varName]; let varValue: o.Expression; @@ -225,27 +220,6 @@ export class CompileElement extends CompileNode { varValue = this.renderNode; } this.view.locals.set(varName, varValue); - const varToken = {value: varName}; - queriesWithReads.push( - ...this._getQueriesFor(varToken).map(query => new _QueryWithRead(query, varToken))); - }); - queriesWithReads.forEach((queryWithRead) => { - let value: o.Expression; - if (isPresent(queryWithRead.read.identifier)) { - // query for an identifier - value = this.instances.get(tokenReference(queryWithRead.read)); - } else { - // query for a reference - const token = this.referenceTokens[queryWithRead.read.value]; - if (isPresent(token)) { - value = this.instances.get(tokenReference(token)); - } else { - value = this.elementRef; - } - } - if (isPresent(value)) { - queryWithRead.query.addValue(value, this.view); - } }); } @@ -264,12 +238,14 @@ export class CompileElement extends CompileNode { this.view.injectorGetMethod.addStmt(createInjectInternalCondition( this.nodeIndex, providerChildNodeCount, resolvedProvider, providerExpr)); }); + } + finish() { Array.from(this._queries.values()) .forEach( queries => queries.forEach( - q => - q.afterChildren(this.view.createMethod, this.view.updateContentQueriesMethod))); + q => q.generateStatements( + this.view.createMethod, this.view.updateContentQueriesMethod))); } addContentNode(ngContentIndex: number, nodeExpr: CompileViewRootNode) { @@ -282,12 +258,11 @@ export class CompileElement extends CompileNode { null; } - getProviderTokens(): o.Expression[] { - return Array.from(this._resolvedProviders.values()) - .map((resolvedProvider) => createDiTokenExpression(resolvedProvider.token)); + getProviderTokens(): CompileTokenMetadata[] { + return Array.from(this._resolvedProviders.values()).map(provider => provider.token); } - private _getQueriesFor(token: CompileTokenMetadata): CompileQuery[] { + getQueriesFor(token: CompileTokenMetadata): CompileQuery[] { const result: CompileQuery[] = []; let currentEl: CompileElement = this; let distance = 0; @@ -425,10 +400,3 @@ function createProviderProperty( } return o.THIS_EXPR.prop(propName); } - -class _QueryWithRead { - public read: CompileTokenMetadata; - constructor(public query: CompileQuery, match: CompileTokenMetadata) { - this.read = query.meta.read || match; - } -} diff --git a/modules/@angular/compiler/src/view_compiler/compile_query.ts b/modules/@angular/compiler/src/view_compiler/compile_query.ts index 6f9a2b1aa4741..db53a53355009 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_query.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_query.ts @@ -64,7 +64,7 @@ export class CompileQuery { return !this._values.values.some(value => value instanceof ViewQueryValues); } - afterChildren(targetStaticMethod: CompileMethod, targetDynamicMethod: CompileMethod) { + generateStatements(targetStaticMethod: CompileMethod, targetDynamicMethod: CompileMethod) { const values = createQueryValues(this._values); const updateStmts = [this.queryList.callMethod('reset', [o.literalArr(values)]).toStmt()]; if (isPresent(this.ownerDirectiveExpression)) { diff --git a/modules/@angular/compiler/src/view_compiler/compile_view.ts b/modules/@angular/compiler/src/view_compiler/compile_view.ts index c1d999f5962f6..e4b8bfd1c7e9f 100644 --- a/modules/@angular/compiler/src/view_compiler/compile_view.ts +++ b/modules/@angular/compiler/src/view_compiler/compile_view.ts @@ -154,11 +154,11 @@ export class CompileView implements NameResolver { } } - afterNodes() { + finish() { Array.from(this.viewQueries.values()) .forEach( queries => queries.forEach( - q => q.afterChildren(this.createMethod, this.updateViewQueriesMethod))); + q => q.generateStatements(this.createMethod, this.updateViewQueriesMethod))); } } diff --git a/modules/@angular/compiler/src/view_compiler/query_binder.ts b/modules/@angular/compiler/src/view_compiler/query_binder.ts new file mode 100644 index 0000000000000..9f2ddb31fb48c --- /dev/null +++ b/modules/@angular/compiler/src/view_compiler/query_binder.ts @@ -0,0 +1,55 @@ +/** + * @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 {CompileQueryMetadata, CompileTokenMetadata, tokenReference} from '../compile_metadata'; +import * as o from '../output/output_ast'; + +import {CompileElement} from './compile_element'; +import {CompileQuery} from './compile_query'; + + +// Note: We can't do this when we create the CompileElements already, +// as we create embedded views before the