From 6fda97287ebd102372a09fe3466a405b317a49c1 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Tue, 1 Nov 2016 14:21:40 -0700 Subject: [PATCH] fix(compiler): support multiple components in a view container --- .../src/view_compiler/view_builder.ts | 2 +- .../core/test/linker/integration_spec.ts | 36 +++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/modules/@angular/compiler/src/view_compiler/view_builder.ts b/modules/@angular/compiler/src/view_compiler/view_builder.ts index 0590c2bbc7..2f5186d5cb 100644 --- a/modules/@angular/compiler/src/view_compiler/view_builder.ts +++ b/modules/@angular/compiler/src/view_compiler/view_builder.ts @@ -42,7 +42,7 @@ export function buildView( const parentEl = view.declarationElement.isNull() ? view.declarationElement : view.declarationElement.parent; templateVisitAll(builderVisitor, template, parentEl); - if (view.viewType === ViewType.EMBEDDED) { + if (view.viewType === ViewType.EMBEDDED || view.viewType === ViewType.HOST) { view.lastRenderNode = builderVisitor.getOrCreateLastRenderNode(); } return builderVisitor.nestedViewCount; diff --git a/modules/@angular/core/test/linker/integration_spec.ts b/modules/@angular/core/test/linker/integration_spec.ts index 9458187ac6..80f76b6d9c 100644 --- a/modules/@angular/core/test/linker/integration_spec.ts +++ b/modules/@angular/core/test/linker/integration_spec.ts @@ -972,11 +972,24 @@ function declareTests({useJit}: {useJit: boolean}) { .createComponent(MyComp); var tc = fixture.debugElement.children[0].children[0]; var dynamicVp: DynamicViewport = tc.injector.get(DynamicViewport); - dynamicVp.done.then((_) => { - fixture.detectChanges(); - expect(fixture.debugElement.children[0].children[1].nativeElement) - .toHaveText('dynamic greet'); - }); + dynamicVp.create(); + fixture.detectChanges(); + expect(fixture.debugElement.children[0].children[1].nativeElement) + .toHaveText('dynamic greet'); + })); + + it('should allow to create multiple ViewContainerRef at a location', async(() => { + var fixture = TestBed.configureTestingModule({schemas: [NO_ERRORS_SCHEMA]}) + .createComponent(MyComp); + var tc = fixture.debugElement.children[0].children[0]; + var dynamicVp: DynamicViewport = tc.injector.get(DynamicViewport); + dynamicVp.create(); + dynamicVp.create(); + fixture.detectChanges(); + expect(fixture.debugElement.children[0].children[1].nativeElement) + .toHaveText('dynamic greet'); + expect(fixture.debugElement.children[0].children[2].nativeElement) + .toHaveText('dynamic greet'); })); }); @@ -1657,17 +1670,18 @@ class SimpleImperativeViewComponent { @Directive({selector: 'dynamic-vp'}) class DynamicViewport { - done: Promise; - constructor(vc: ViewContainerRef, componentFactoryResolver: ComponentFactoryResolver) { + private componentFactory: ComponentFactory; + private injector: Injector; + constructor(private vc: ViewContainerRef, componentFactoryResolver: ComponentFactoryResolver) { var myService = new MyService(); myService.greeting = 'dynamic greet'; - var injector = ReflectiveInjector.resolveAndCreate( + this.injector = ReflectiveInjector.resolveAndCreate( [{provide: MyService, useValue: myService}], vc.injector); - this.done = - Promise.resolve(componentFactoryResolver.resolveComponentFactory(ChildCompUsingService)) - .then((componentFactory) => vc.createComponent(componentFactory, 0, injector)); + this.componentFactory = componentFactoryResolver.resolveComponentFactory(ChildCompUsingService); } + + create() { this.vc.createComponent(this.componentFactory, this.vc.length, this.injector); } } @Directive({selector: '[my-dir]', inputs: ['dirProp: elprop'], exportAs: 'mydir'})