简体   繁体   English

错误设置宽度 - 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改

[英]Error setting width - Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

I use a kendo-grid to display some data.我使用kendo-grid来显示一些数据。 I use [width] property to dynamically apply the desired width to each column.我使用[width]属性将所需的宽度动态应用于每一列。 I have a columnsWidths: Array<{ field: string, width: number }> to achieve the result.我有一个columnsWidths: Array<{ field: string, width: number }>来实现结果。

html html

<kendo-grid-column field="Title" title="{{lbl_ColTitle}}" [hidden]="isHidden('Title')" [width]="isWidth('Title')" class="editableGridCell" [editable]="!APsService.isLoading && isPendingDocument && isGDPRApproved && !isTemplate && isAllowedToUpdate">
     <ng-template kendoGridCellTemplate let-dataItem>
         {{dataItem.Title}}
     </ng-template>
</kendo-grid-column>
<kendo-grid-column field="Description" title="{{lbl_ColDescription}}" [hidden]="isHidden('Description')" [width]="isWidth('Description')" class="editableGridCell" [editable]="!APsService.isLoading && isPendingDocument && isGDPRApproved && !isTemplate && isAllowedToUpdate">
     <ng-template kendoGridCellTemplate let-dataItem>
         {{dataItem.Description}}
     </ng-template>
</kendo-grid-column>

ts ts

public isWidth(columnName: string): number {
    return this.columnsWidths.findIndex((item: any) => item.field === columnName) > -1 && this.columnsWidths.filter((item: any) => item.field === columnName)[0].width;   
}

Everything looks great using the code above.使用上面的代码,一切看起来都很棒。 However, I would like to slightly modify the width in a specific case (when a column's width is greater than the available grid width).但是,我想在特定情况下稍微修改宽度(当列的宽度大于可用的网格宽度时)。 So, I've changed my code to:因此,我将代码更改为:

public isWidth(columnName: string): number {
    if (this.columnsWidths.filter((item: any) => item.width >= this.gridView.wrapper.nativeElement.offsetWidth).length > 0) {         
        return this.columnsWidths.findIndex((item: any) => item.field === columnName) > -1 && this.columnsWidths.filter((item: any) => item.field === columnName)[0].width * 0.7;
    }
    else {
        return this.columnsWidths.findIndex((item: any) => item.field === columnName) > -1 && this.columnsWidths.filter((item: any) => item.field === columnName)[0].width;
    }
}

The actual result displayed is the right one (column widths seem to be applied correctly), but the following error comes up in my console:显示的实际结果是正确的(列宽似乎已正确应用),但我的控制台中出现以下错误:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: 'width: 641.1999999999999'.以前的值:'宽度:641.1999999999999'。 Current value: 'width: 916'.当前值:'宽度:916'。

I did try to console.log to find out what's going on in the isWidth method and found out that the first condition is initially met (it shouldn't as the saved setting is {field: "Description", width: 916} and the gridView.wrapper.nativeElement.offsetWidth is 1830) and, after that, the second one is continuously met (as expected).我确实尝试通过console.log找出isWidth方法中发生了什么,并发现最初满足第一个条件(它不应该因为保存的设置是{field: "Description", width: 916}gridView.wrapper.nativeElement.offsetWidth为 1830),之后不断满足第二个(如预期的那样)。

How can I avoid this error?我怎样才能避免这个错误? Thank you.谢谢你。

I tried to find a solution reading a lot of stuff from many sources.我试图找到一个解决方案,从许多来源阅读很多东西。 Trying several workarounds, I found out that the solution in my case is the following:尝试了几种解决方法,我发现我的解决方案如下:

ngAfterViewInit() {
    this.ref.detectChanges(); <--- this is the only code I added 
    setTimeout(() => {
        this.calcPageSize();
    });
    window.addEventListener('resize', this.calcPageSizeOnResize);
}

However, using concole.log I realized that it behaves in the same way.但是,使用concole.log我意识到它的行为方式相同。 The first condition is initially met and, after that, the second one is met for the rest of the component's life.最初满足第一个条件,然后满足第二个条件,以保证组件的使用寿命。 The difference is that the error disappeared from my console output.不同之处在于错误从我的控制台 output 中消失了。

I hope there is no bad effect occurring from this addition and nothing else changes in an undesired way.我希望这种添加不会产生不良影响,并且不会以不希望的方式发生其他任何变化。

This helped me a lot https://stackoverflow.com/a/52922389/9880356 and here you can find a really useful source: https://medium.com/angular-in-depth/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4这对我帮助很大知道表达式在检查后更改错误-错误-e3fd9ce7dbb4

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked Angular:错误错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Angular: ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Angular - ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Angular Angular 中的“ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改”错误 - "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked" error in Angular 计算得到错误的值:ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked - calcuate value getting the error : ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 面临错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Facing Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked Angular7 + REDUX:错误:“ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Angular7 + REDUX: Error: "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 未捕获的错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Uncaught Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 自定义图例时出现错误“ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked” - Getting Error “ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked” on customizing legends
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM