Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Typescript Angular 2向已经实例化的动态加载组件提供新数据_Typescript_Angular - Fatal编程技术网

Typescript Angular 2向已经实例化的动态加载组件提供新数据

Typescript Angular 2向已经实例化的动态加载组件提供新数据,typescript,angular,Typescript,Angular,我使用的是DynamicComponentLoader,如果所有数据都是在组件初始化时启动的,那么一切都正常工作。我正在使用以下代码: this.dcl.loadAsRoot(SomeComponent, "#somediv", this.injector).then((cmp)=>{ // @Input() cmp.instance.someinput = this.someinput; // @Output() cmp.instance.so

我使用的是DynamicComponentLoader,如果所有数据都是在组件初始化时启动的,那么一切都正常工作。我正在使用以下代码:

this.dcl.loadAsRoot(SomeComponent, "#somediv", this.injector).then((cmp)=>{

     // @Input()
     cmp.instance.someinput = this.someinput;

     // @Output()
     cmp.instance.someoutput.subscribe(res => { this.consoleLog(res) })

     // Trigger change detection
     cmp.location.internalElement.parentView.changeDetector.ref.detectChanges();

    // return data
    return cmp

})

我现在想给“someinput”变量输入新值,这是可能的还是我需要重新初始化整个组件

只需保留对组件的引用即可

compRef:ComponentRef;

someFunc() {
  this.dcl.loadAsRoot(SomeComponent, "#somediv", this.injector).then((cmp)=>{
    this.compRef = cmp;
    ...
  }
}

someOtherFunc() {
  this.compRef.someInput = 'someotherValue';
  this.compRef.location.internalElement.parentView.changeDetector
      .ref.detectChange‌​s();
}

您不应该使用loadAsRoot。使用loadNextToLocation或loadIntoLocation.Good,您只是忘记了:this.compRef.location.internalElement.parentView.changeDetector.ref.detectChanges();