相关疑难解决方法(0)

动态创建具有ng-content的angular2组件

我想设置<ng-content>动态实例化组件的主体ComponentFactoryResolver.

我看到我可以使用输入和输出访问ComponentRef,但不能设置<ng-content>.

请注意<ng-content>我计划设置可以包含简单文本/可以跨越动态创建的组件

@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 …
Run Code Online (Sandbox Code Playgroud)

angular-components angular

12
推荐指数
1
解决办法
6924
查看次数

标签 统计

angular ×1

angular-components ×1