我知道在stackoverflow中已經發布了很多相同的問題,並嘗試了不同的解決方案來避免運行時錯誤,但是它們都不適用於我。ExpressionChangedAfterItHasBeenCheckedError:檢查後表達式已更改。以前的值:'undefined'
組件和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像下面我收到此相同error.Just添加一個角度動態加載ngclass屬性
<div class="container-fluid ds-iconwidget-wrapper text-center" [ngClass]="Sample">
</div>
我的數據綁定和一切工作都很好。我需要對父組件做任何事情嗎? 我已經嘗試了子組件中的所有生命週期事件
你怎麼加上'TestComponent'的內容? –
@Maximus作爲條目組件 – JEMI
@Maximus我試着調用this.cdRef.detectChanges();明確但不起作用 – JEMI