Angularjs 角度2 N通过元件参考设定值时不触发
我以这种方式动态创建组件:Angularjs 角度2 N通过元件参考设定值时不触发,angularjs,dynamic,ngonchanges,Angularjs,Dynamic,Ngonchanges,我以这种方式动态创建组件: let componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.component); this.componentRef = this.viewContainerRef.createComponent(componentFactory); this.componentRef.instance.text = this.text; 如您所见,我还为“text”属性指定
let componentFactory = this._componentFactoryResolver.resolveComponentFactory(this.component);
this.componentRef = this.viewContainerRef.createComponent(componentFactory);
this.componentRef.instance.text = this.text;
如您所见,我还为“text”属性指定了一个值。这是可行的,但它不会在目标组件中触发ngOnChanges,因此也不会触发更改检测(我使用的是“推”策略)
我试着用手触发它
this.componentRef.changeDetectorRef.detectChanges();
及
但是没有成功
有没有办法更好地将新的文本值分配给目标组件@Input('text')?文本只是一个字符串值
更新:
现在,我在目标组件中使用setter构建了一个变通方法:
@Input('text')
set text(text: string) {
this._text = text;
this.cdRef.markForCheck();
}
它可以工作,但有点粗糙,我还是更喜欢“角度”的方式。它真的工作吗?我认为它不管用!如果你有,你能提供一个工作链接吗?
@Input('text')
set text(text: string) {
this._text = text;
this.cdRef.markForCheck();
}