ComponentFactoryResolver
'u kullanarak bir bileşeni dinamik olarak başlatırken <ng-content>
gövdesini ayarlamak istiyorum.Dinamik olarak ng içeriğiyle bir açısal2 bileşeni oluşturma
girdisi ile numaralı çıktıya erişim sağlayabildiğimi görüyorum, ancak <ng-content>
'u ayarlamak için bir yol değil.
basit metin içerebilir ben ayarına planlıyorum <ng-content>
unutmayınız/dinamik olarak oluşturulan yayılabilir bileşenler
@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 {}
"projectableNodes" parametresini kullan https://stackoverflow.com/questions/41372334/why-is-projectablenodes-an-any – yurzui
Dinamik bir bileşeni de 'projectableNodes' olarak ekleyebilirim, böylece çocuk yönergesinin ebeveyni olarak kullanılabilir. 'ContentChild' @? –
Projeksiyon yapılabilir bir 'Köşe' olduğundan, sadece 'DOM' öğelerini –