Typescript Angular 2向已经实例化的动态加载组件提供新数据
我使用的是DynamicComponentLoader,如果所有数据都是在组件初始化时启动的,那么一切都正常工作。我正在使用以下代码: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
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.detectChanges();
}
您不应该使用loadAsRoot。使用loadNextToLocation或loadIntoLocation.Good,您只是忘记了:this.compRef.location.internalElement.parentView.changeDetector.ref.detectChanges();