Skip to content

Commit

Permalink
fix(compiler): Allow templates to access variables that are declared …
Browse files Browse the repository at this point in the history
…afterwards.

Fixes #8261
  • Loading branch information
tbosch committed Apr 27, 2016
1 parent c209836 commit 1e8864c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions modules/angular2/src/compiler/view_compiler/view_binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ViewBinderVisitor implements TemplateAstVisitor {
bindDirectiveDestroyLifecycleCallbacks(directiveAst.directive, directiveInstance,
compileElement);
});
bindView(compileElement.embeddedView, ast.children);
return null;
}

Expand Down
29 changes: 13 additions & 16 deletions modules/angular2/src/compiler/view_compiler/view_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ import {
CompileTokenMetadata
} from '../compile_metadata';

import {bindView} from './view_binder';

const IMPLICIT_TEMPLATE_VAR = '\$implicit';
const CLASS_ATTR = 'class';
const STYLE_ATTR = 'style';
Expand All @@ -65,28 +63,28 @@ export class ViewCompileDependency {
}

export function buildView(view: CompileView, template: TemplateAst[],
targetDependencies: ViewCompileDependency[],
targetStatements: o.Statement[]): number {
var builderVisitor = new ViewBuilderVisitor(view, targetDependencies, targetStatements);
targetDependencies: ViewCompileDependency[]): number {
var builderVisitor = new ViewBuilderVisitor(view, targetDependencies);
templateVisitAll(builderVisitor, template, view.declarationElement.isNull() ?
view.declarationElement :
view.declarationElement.parent);
// Need to separate binding from creation to be able to refer to
// variables that have been declared after usage.
bindView(view, template);
view.afterNodes();

createViewTopLevelStmts(view, targetStatements);

return builderVisitor.nestedViewCount;
}

export function finishView(view: CompileView, targetStatements: o.Statement[]) {
view.afterNodes();
createViewTopLevelStmts(view, targetStatements);
view.nodes.forEach((node) => {
if (node instanceof CompileElement && node.hasEmbeddedView) {
finishView(node.embeddedView, targetStatements);
}
});
}

class ViewBuilderVisitor implements TemplateAstVisitor {
nestedViewCount: number = 0;

constructor(public view: CompileView, public targetDependencies: ViewCompileDependency[],
public targetStatements: o.Statement[]) {}
constructor(public view: CompileView, public targetDependencies: ViewCompileDependency[]) {}

private _isRootNode(parent: CompileElement): boolean { return parent.view !== this.view; }

Expand Down Expand Up @@ -284,8 +282,7 @@ class ViewBuilderVisitor implements TemplateAstVisitor {
var embeddedView = new CompileView(
this.view.component, this.view.genConfig, this.view.pipeMetas, o.NULL_EXPR,
this.view.viewIndex + this.nestedViewCount, compileElement, templateVariableBindings);
this.nestedViewCount +=
buildView(embeddedView, ast.children, this.targetDependencies, this.targetStatements);
this.nestedViewCount += buildView(embeddedView, ast.children, this.targetDependencies);

compileElement.beforeChildren();
this._addRootNodeAndProject(compileElement, ast.ngContentIndex, parent);
Expand Down
10 changes: 8 additions & 2 deletions modules/angular2/src/compiler/view_compiler/view_compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {Injectable} from 'angular2/src/core/di';
import * as o from '../output/output_ast';
import {CompileElement} from './compile_element';
import {CompileView} from './compile_view';
import {buildView, ViewCompileDependency} from './view_builder';
import {buildView, finishView, ViewCompileDependency} from './view_builder';
import {bindView} from './view_binder';

import {CompileDirectiveMetadata, CompilePipeMetadata} from '../compile_metadata';

Expand All @@ -25,7 +26,12 @@ export class ViewCompiler {
var dependencies = [];
var view = new CompileView(component, this._genConfig, pipes, styles, 0,
CompileElement.createNull(), []);
buildView(view, template, dependencies, statements);
buildView(view, template, dependencies);
// Need to separate binding from creation to be able to refer to
// variables that have been declared after usage.
bindView(view, template);
finishView(view, statements);

return new ViewCompileResult(statements, view.viewFactory.name, dependencies);
}
}
10 changes: 4 additions & 6 deletions modules/angular2/test/core/linker/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,16 @@ function declareTests(isJit: boolean) {
(tcb: TestComponentBuilder, async) => {
tcb.overrideView(
MyComp, new ViewMetadata({
template: '<p>{{alice.ctxProp}}<child-cmp var-alice></child-cmp></p>',
directives: [ChildComp]
template:
'<template [ngIf]="true">{{alice.ctxProp}}</template>|{{alice.ctxProp}}|<child-cmp var-alice></child-cmp>',
directives: [ChildComp, NgIf]
}))

.createAsync(MyComp)
.then((fixture) => {
fixture.detectChanges();

expect(fixture.debugElement.nativeElement)
.toHaveText('hellohello'); // this first one is the
// component, the second one is
// the text binding
expect(fixture.debugElement.nativeElement).toHaveText('hello|hello|hello');
async.done();
})}));

Expand Down

0 comments on commit 1e8864c

Please sign in to comment.