Skip to content

Commit 2f015cc

Browse files
committed
fix(view_container): fixes injection on dynamically added views.
Closes angular#777
1 parent 9b08ab3 commit 2f015cc

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

modules/angular2/src/core/compiler/view_container.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ export class ViewContainer {
7474
'Cannot create views on a dehydrated ViewContainer');
7575
// TODO(rado): replace with viewFactory.
7676
var newView = this.defaultProtoView.instantiate(this.hostElementInjector, this._eventManager);
77+
// insertion must come before hydration so that element injector trees are attached.
78+
this.insert(newView, atIndex);
7779
newView.hydrate(this.appInjector, this.hostElementInjector, this.parentView.context);
78-
return this.insert(newView, atIndex);
80+
return newView;
7981
}
8082

8183
insert(view, atIndex=-1): viewModule.View {

modules/angular2/test/core/compiler/integration_spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import {Decorator, Component, Viewport} from 'angular2/src/core/annotations/anno
1919
import {Template} from 'angular2/src/core/annotations/template';
2020
import {Parent, Ancestor} from 'angular2/src/core/annotations/visibility';
2121

22+
import {If} from 'angular2/src/directives/if';
23+
2224
import {ViewContainer} from 'angular2/src/core/compiler/view_container';
2325

2426
export function main() {
@@ -365,6 +367,29 @@ export function main() {
365367
done();
366368
})
367369
});
370+
371+
it('should create a component that injects an @Ancestor through viewport directive', (done) => {
372+
tplResolver.setTemplate(MyComp, new Template({
373+
inline: `
374+
<some-directive>
375+
<p *if="true">
376+
<cmp-with-ancestor #child></cmp-with-ancestor>
377+
</p>
378+
</some-directive>`,
379+
directives: [SomeDirective, CompWithAncestor, If]
380+
}));
381+
382+
compiler.compile(MyComp).then((pv) => {
383+
createView(pv);
384+
cd.detectChanges();
385+
386+
var subview = view.viewContainers[0].get(0);
387+
var childComponent = subview.contextWithLocals.get('child');
388+
expect(childComponent.myAncestor).toBeAnInstanceOf(SomeDirective);
389+
390+
done();
391+
})
392+
});
368393
});
369394

370395
// TODO support these tests with DART e.g. with Promise.catch (JS) transpiled to Future.catchError (DART)

0 commit comments

Comments
 (0)