Criando um componente angular2 dinamicamente com conteúdo ng
Eu gostaria de definir o corpo de<ng-content>
instanciando um componente dinamicamente usandoComponentFactoryResolver
.
Vejo que posso obter acesso à entrada e saída usandoComponentRef
, mas não é uma maneira de definir<ng-content>
.
Observe<ng-content>
Estou pensando em configuração pode conter texto simples / pode abranger componentes criados dinamicamente
@Component({
selector: 'app-component-to-project',
template: `<ng-content></ng-content>`
})
export class ComponentToProject implements AfterContentInit {
ngAfterContentInit() {
// We will do something important with content here
}
}
@Directive({
selector: 'appProjectionMarker'
})
export class ProjectionMarkerDirective implements OnInit {
constructor(private viewContainerRef: ViewContainerRef, private componentFactoryResolver: ComponentFactoryResolver) {
}
ngOnInit() {
const componentFactory: ComponentFactory<ComponentToProject> = this.componentFactoryResolver.resolveComponentFactory(ComponentToProject);
const componentRef: ComponentRef<ComponentToProject> = this.viewContainerRef.createComponent(componentFactory);
// Question: How to set content before the child's afterContentInit is invoked
}
}
@Component({
selector: 'appTestComponent',
template: `<div appProjectionMarker></div>`
})
export class TestComponent {}