【发布时间】:2018-01-10 02:16:41
【问题描述】:
我知道堆栈溢出中已经发布了很多相同的问题,并尝试了不同的解决方案来避免运行时错误,但没有一个对我有用。
组件和 Html 代码
export class TestComponent implements OnInit, AfterContentChecked {
@Input() DataContext: any;
@Input() Position: any;
sampleViewModel: ISampleViewModel = { DataContext: : null, Position: null };
constructor(private validationService: IValidationService, private modalService: NgbModal, private cdRef: ChangeDetectorRef) {
}
ngOnInit() {
}
ngAfterContentChecked() {
debugger;
this.sampleViewModel.DataContext = this.DataContext;
this.sampleViewModel.Position = this.Position;
}
<div class="container-fluid sample-wrapper text-center" [ngClass]="sampleViewModel.DataContext?.Style?.CustomCssClass +' samplewidget-'+ sampleViewModel.Position?.Columns + 'x' + sampleViewModel.Position?.Rows">
//some other html here
</div>
请注意:此组件是使用 DynamicComponentLoader 动态加载的
在解决问题后,我发现了几个问题
首先,这个子组件是通过使用 DynamicComponentResolver 动态加载并传递如下输入值
ngAfterViewInit() {
this.renderWidgetInsideWidgetContainer();
}
renderWidgetInsideWidgetContainer() {
let component = this.storeFactory.getWidgetComponent(this.dataSource.ComponentName);
let componentFactory = this._componentFactoryResolver.resolveComponentFactory(component);
let viewContainerRef = this.widgetHost.viewContainerRef;
viewContainerRef.clear();
let componentRef = viewContainerRef.createComponent(componentFactory);
debugger;
(<IDataBind>componentRef.instance).WidgetDataContext = this.dataSource.DataContext;
(<IDataBind>componentRef.instance).WidgetPosition = this.dataSource.Position;
}
即使我像下面这样更改了我的子组件 html,我也会遇到同样的错误。只需添加一个角度 ngclass 属性
<div class="container-fluid ds-iconwidget-wrapper text-center" [ngClass]="Sample">
</div>
我的数据绑定和一切工作正常。我需要对父组件做任何事情吗? 我已经尝试过子组件中的所有生命周期事件
【问题讨论】:
-
如何添加
TestComponent? -
首先将
this.renderWidgetInsideWidgetContainer();从ngAfterViewInit移动到ngOnInit -
@Maximus 感谢您的时间..您的文章很棒
-
@JEMI,不客气)我有很多有趣的文章,请检查
-
@Maximus 当然。我现在已经是你的追随者了。你的每一篇文章都很有深度。
标签: angular runtime-error