最佳答案
我知道在堆栈溢出中已经发布了很多相同的问题,并且尝试了不同的解决方案来避免运行时错误,但是没有一个对我有用。
组件和 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>
Please Note : This Component is loaded dynamically using DynamicComponentLoader
After My trouble shooting I have identified couple of issues
首先,通过使用 DynamicComponent entResolver 并传递下面这些输入值,可以动态加载这个子组件
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>
我的数据绑定和一切工作正常。我需要对父组件执行任何操作吗? 我已经尝试了子组件中的所有生命周期事件