gpt4 book ai didi

Angular 错误 : ExpressionChangedAfterItHasBeenCheckedError

转载 作者:太空狗 更新时间:2023-10-29 18:06:59 27 4
gpt4 key购买 nike

我在使用 Angular 4 时遇到了一点问题。抛出以下错误

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '225px'. Current value: '-21150px'.

这是我的 html:

<div class="channelRow" *ngFor="let epgChannel of channelGroupEpg?.epgChannels">
<div class="epgChannelList">
{{epgChannel.channel.name}}
</div>
<template ngFor let-epgDate [ngForOf]="epgChannel.dates">
<div class="epgEntry"
*ngFor="let epgProgram of epgDate.programs"
[style.width]="getWidth(epgProgram, epgDate.date)"
(click)="selectProgram(epgProgram)"
data-toggle="modal"
data-target="#detailModal">
{{epgProgram.title}}
</div>
</template>
</div>

这就是错误的来源:

getWidth(program: EPGProgram, date: Date) {
let min = this.helperService.getDurationByProgramInMinutes(program);
let startDate = new Date(program.start.getFullYear(), program.start.getMonth(), program.start.getDate(), 0, 0, 0, 0);
let stopDate = new Date(program.stop.getFullYear(), program.stop.getMonth(), program.stop.getDate(), 0, 0, 0, 0);
if (startDate.getTime() !== date.getTime()) {
let minNextDay = this.helperService.getMinutesToNextDay(program.start, date);
min = min - minNextDay;
}
if (stopDate.getTime() !== date.getTime()) {
let minNextDay = this.helperService.getMinutesToLastDay(program.stop, date);
min = min - minNextDay;
}
let width = min * 5;
return width.toString() + 'px';
}

谁能告诉我,为什么会这样?

最佳答案

组件状态在检查后发生了变化......

使用

this._changeDetectionRef.detectChanges();

在你的方法的最后,不要忘记添加

private _changeDetectionRef : ChangeDetectorRef

作为拥有您方法的组件的构造函数的参数。

参见讨论 here

关于 Angular 错误 : ExpressionChangedAfterItHasBeenCheckedError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44017756/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com