Skip to content

Instantly share code, notes, and snippets.

@arturovt
Last active July 28, 2019 09:42
Show Gist options
  • Save arturovt/7c03685b430bee62fcc6b1a381ceb6a1 to your computer and use it in GitHub Desktop.
Save arturovt/7c03685b430bee62fcc6b1a381ceb6a1 to your computer and use it in GitHub Desktop.
import {
Component,
ChangeDetectionStrategy,
ɵcreateInjector as createInjector,
Injector,
ViewChild,
ViewContainerRef
} from '@angular/core';
@Component({
selector: 'app-root',
template: `
<ng-container #carousel></ng-container>
<button (click)="showCarousel()">Show carousel</button>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
@ViewChild('carousel', { read: ViewContainerRef, static: true })
public carousel: ViewContainerRef;
constructor(private injector: Injector) {}
public showCarousel(): void {
import('./carousel/carousel.module').then(({ CarouselModule }) => {
const injector = createInjector(CarouselModule, this.injector);
const carouselModule = injector.get(CarouselModule);
const componentFactory = carouselModule.resolveCarouselComponentFactory();
const componentRef = this.carousel.createComponent(componentFactory);
componentRef.changeDetectorRef.markForCheck();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment