Last active
July 29, 2019 19:03
-
-
Save arturovt/c4dd813eb401c9c21efac7d97b05a86a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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