简体   繁体   English

角度错误:ExpressionChangedAfterItHasBeenCheckedError

[英]Angular Error: ExpressionChangedAfterItHasBeenCheckedError

I have a little problem with Angular 4. The error below is thrown 我对Angular 4有一点问题。抛出以下错误

Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: '225px'. 之前的价值:'225px'。 Current value: '-21150px'. 当前价值:' - 215050x'。

Here's my html: 这是我的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>

And this is where the error comes from: 这就是错误的来源:

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';
}

Can someone tell me, why this is the case? 有人能告诉我,为什么会这样?

Component state was changed after it was checked ... 组件状态在检查后发生了变化......

use 采用

this._changeDetectionRef.detectChanges();

at the end of your method, not forgetting to add 在你的方法结束时,不要忘记添加

private _changeDetectionRef : ChangeDetectorRef

as parameter of the constructor of the Component owning your method. 作为拥有您的方法的Component的构造函数的参数。

See discution here 请参阅这里的讨论

This error occurs because you are changing the variable before the angular ouput the HTML. 发生此错误的原因是您在角度输出HTML之前更改变量。 So, what you can do is. 所以,你能做的是。 change your variable when the content is loaded. 加载内容时更改变量。 solution. 解。

import { AfterContentInit } from '@angular/core';

ngAfterContentInit(){
   // write your code here
}

快速解决方法是将代码放在setTimeout()中。

I had same error outputed. 我输出了同样的错误。 After long research I found out that it's caused when the data that are inputted to html template has changed before angular refresh the html. 经过长时间的研究,我发现它是在输入到html模板的数据在角度刷新html之前发生变化时引起的。 This error won't be displayed when you switch to production mode. 切换到生产模式时,不会显示此错误。 See this issue for more information. 有关更多信息,请参阅此问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM