我知道在堆栈溢出中已经发布了很多相同的问题,并尝试了不同的解决方案来避免运行时错误,但是没有一个对我有用。
组件和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
?