Angular 2单向数据绑定在动态加载组件内部不会刷新

时间:2016-04-06 08:42:59

标签: typescript angular

我使用了一个只切换标签的组件,一切正常。

在动态组件加载组件中移动组件后,当我使用setTab(){ this.tabInfo = true }中的函数false进行设置时,当我使用{{进行打印时,我在模板中留下了false 1}}。

为什么数据没有更新?我尝试刷新区域,它不起作用。

正在执行该功能,并且所有值都设置正确,我检查了控制台。但是模板中看不到变化。我在构造函数中设置的是固定的,我无法改变它。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您使用loadAsRoot(),则需要明确调用

之类的变更检测
function onYourComponentDispose() {
}
let el = this.elementRef
let reuseInjectorOrCreateNewOne = this.injector;
this.componentLoader.loadAsRoot(YourComponent, this.elementRef, reuseInjectorOrCreateNewOne, onYourComponentDispose)
.then((compRef: ComponentRef) => {
  // manually include Inputs
  compRef.instance['myInputValue'] = {res: 'randomDataFromParent'};
  // manually include Outputs
  compRef.instance['myOutputValue'].subscribe(this.parentObserver)
  // trigger change detection
  cmpRef.location.internalElement.parentView.changeDetector.ref.detectChanges()
  // always return in a promise
  return compRef
});

另见https://github.com/angular/angular/issues/6370#issuecomment-193896657