简体   繁体   English

角度和错误:ExpressionChangedAfterItHasBeenCheckedError

[英]Angular and ERROR: ExpressionChangedAfterItHasBeenCheckedError

I don't know how to fix my problem.我不知道如何解决我的问题。 I have a table with rows In one of the columns there should be displayed some icon (data fetched from the backend service) but I get the following error in the console:我有一个带有行的表在其中一列中应该显示一些图标(从后端服务获取的数据),但我在控制台中收到以下错误:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 Previous value: 'ngIf: undefined'.以前的值:'ngIf:未定义'。 Current value: 'ngIf: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAk当前值: 'ngIf: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAk

Then I added然后我加了

     setLogoImage(logo: any) {
    if (logo) {
      this.imgURL = 'data:image/jpeg;base64,' + logo;
    }
  }

  ngAfterViewChecked(): void {
    this.cdRef.detectChanges();
  }

And my template:还有我的模板:

 <ng-template let-columns="columns" let-rowData pTemplate="body">
    <tr>
      <td class="ui-resizable-column">{{ rowData['name'] || '' }}</td>
      <td class="ui-resizable-column">{{ rowData['active'] | yesNoBoolean }}</td>
      <td class="ui-resizable-column">
       //some data
      </td>
      <td class="ui-resizable-column">{{ setLogoImage(rowData['logo']) }}<img *ngIf="imgURL"
                                                                              [src]="imgURL" height="50"
                                                                              width="50" alt="logo"></td>
      <td class...

Currently icons are displayed without any errors BUT in the wrong order!当前图标显示没有任何错误但顺序错误! Everything is shifted down one row!一切都向下移动了一行! Do you have any idea how to fix that?你知道如何解决这个问题吗?

UPDATE更新

Hmm it doesn't work.嗯它不起作用。 After changes:更改后:

   export class FrameworkSearchResultsComponent extends PaginationComponent implements OnInit, AfterViewChecked {
    
   constructor(private cdRef: ChangeDetectorRef) {
    super();
  }

  ngOnInit() {
    this.pagination.sort = 'name,ASC';
    this.setPage.subscribe(val => {
      if (this.table) {
        this.table.first = val;
      }
    });
  }
    
    
    setLogoImage(logo: any) {
        setTimeout(() => {
          if (logo) {
            this.imgURL = 'data:image/jpeg;base64,' + logo;
          }
        }, 100);
      }
    
      ngAfterViewChecked(): void {
        this.cdRef.detectChanges();
      }

my icons flash very often and wrong icons are display, eg.我的图标经常闪烁并且显示错误的图标,例如。 in row 1 and 2 are displayed the same icons (but in reality are different in each row)第 1 行和第 2 行显示相同的图标(但实际上每行都不同)

Angular runs change detection on component in intervals or on events. Angular 以间隔或事件对组件运行更改检测。 What happened in your case is that you made change to data/view after angular ran it's change detection and before the next change detection cycle.在您的情况下发生的情况是,您在 angular 运行它的更改检测之后和下一个更改检测周期之前对数据/视图进行了更改。

Hence, angular will the change you have made in between.因此,角度将改变您在两者之间所做的更改。 The code will work but it's not guaranteed that it will work 100% of the time.该代码将工作,但不能保证它会在 100% 的时间内工作。

The line that's generating error with you is most possibly this with imgUrl :与您一起生成错误的行很可能是imgUrl
<img *ngIf="imgURL" [src]="imgURL" height="50" width="50" alt="logo">

You need to assign the imgUrl in a change hook like ngOnInit etc or encapsulate it in a settimeout to delay the change for 50 to 100 milleseconds.您需要在诸如ngOnInit等更改挂钩中分配imgUrl或将其封装在settimeout以将更改延迟 50 到 100 毫秒。 You will then not need to run the change detection manually然后您将不需要手动运行更改检测

setLogoImage(logo: any) {
    setTimeout(()=> {
     if (logo) {
      this.imgURL = 'data:image/jpeg;base64,' + logo;
    }
  },100);
}

OK I fixed my problem by changing method setLogoImage to getLogoImage:好的,我通过将方法 setLogoImage 更改为 getLogoImage 来解决我的问题:

  getLogoImage(logo: any) {
    if (logo) {
      return 'data:image/jpeg;base64,' + logo;
    }
  }

And some small changes in template:以及模板的一些小变化:

<td class="ui-resizable-column"><img *ngIf="rowData['logo']"
                                           [src]="getLogoImage(rowData['logo'])" height="50"
                                           width="50" alt="logo"></td>

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

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