Skip to content

Instantly share code, notes, and snippets.

@arturovt
Last active July 29, 2019 19:03
Show Gist options
  • Save arturovt/c4dd813eb401c9c21efac7d97b05a86a to your computer and use it in GitHub Desktop.
Save arturovt/c4dd813eb401c9c21efac7d97b05a86a to your computer and use it in GitHub Desktop.
import {
Component,
ChangeDetectionStrategy,
ViewChild,
ViewContainerRef,
ComponentFactoryResolver,
Injector
} from '@angular/core';
@Component({
selector: 'app-root',
template: `
<ng-container #button></ng-container>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AppComponent {
@ViewChild('button', { read: ViewContainerRef, static: true })
public button: ViewContainerRef;
constructor(resolver: ComponentFactoryResolver, injector: Injector) {
import('./button.component').then(({ ButtonComponent }) => {
const componentFactory = resolver.resolveComponentFactory(ButtonComponent);
const componentRef = this.button.createComponent(componentFactory, 0, injector);
componentRef.instance.text = 'Click me';
componentRef.changeDetectorRef.markForCheck();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment