Angular 构建cdk模式-未触发Ngonit且未传递任何数据
我正在尝试构建只依赖于Angular 构建cdk模式-未触发Ngonit且未传递任何数据,angular,angular-cdk,Angular,Angular Cdk,我正在尝试构建只依赖于CDK的modals库。 模态是用服务打开的,我通过entryComponent在模态中呈现它 以下是示例: 在模态本身中,我使用factory创建组件: const compRef = this.componentFactoryResolver.resolveComponentFactory<any>(this.modalContentComponent); const componentRef = this.modalContainer.createComp
CDK
的modals
库。
模态是用服务打开的,我通过entryComponent
在模态中呈现它
以下是示例:
在模态本身中,我使用factory创建组件:
const compRef = this.componentFactoryResolver.resolveComponentFactory<any>(this.modalContentComponent);
const componentRef = this.modalContainer.createComponent(compRef);
const compRef=this.componentFactoryResolver.resolveComponentFactory(this.modalContentComponent);
const componentRef=this.modalContainer.createComponent(compRef);
我有两个问题:
componentRef.instance.ngOnInit()代码>手动
componentRef.instance.name=this.data.name代码>但组件从不渲染它
在
modal.component.ts
中,使用ngOnInit
而不是ngAfterViewInit
:
ngOnInit() {
const compRef = this.componentFactoryResolver.resolveComponentFactory<any>(this.modalContentComponent);
const componentRef = this.modalContainer.createComponent(compRef);
componentRef.instance.name = this.data.name;
}
ngOnInit(){
const compRef=this.componentFactoryResolver.resolveComponentFactory
这样做意味着将为您运行并检测更改,因为ngDoCheck
直接在ngOnInit
之后运行-非常感谢。我想我必须在视图初始化时执行此操作。很好,这太棒了!