Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 构建cdk模式-未触发Ngonit且未传递任何数据_Angular_Angular Cdk - Fatal编程技术网

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

    之后运行-非常感谢。我想我必须在视图初始化时执行此操作。很好,这太棒了!