简体   繁体   English

ExpressionChangedAfterItHasBeenCheckedError:使用Angular2检查表达式后,表达式已更改

[英]ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2

i had tried to solve this error ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'. 我曾尝试解决此错误ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'. ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '0'. in this way: 通过这种方式:

TS: TS:

 import {
     ChangeDetectionStrategy
 } from '@angular/core';
 @Component({
     templateUrl: './page.html'
     changeDetection: ChangeDetectionStrategy.OnPush
 })
 public Pages: Object;

 ngOnInit() {
     loadAllGuides() {
         this.ApiService
             .getAllPages()
             .subscribe(
                 Pages => {
                     this.Pages = Pages.data;
                 }, error => {
                     console.log(error);
                 });
     }
 }

When i used ChangeDetectionStrategy the error got solved in console but i am facing one more issue, the page is not showing any data unless i give a click when the page opens. 当我使用ChangeDetectionStrategy该错误已在控制台中解决,但我面临另一个问题,除非页面打开时我单击一下,否则该页面未显示任何数据。 Once i give a random click on page the data loads. 一旦我在页面上随机单击,数据就会加载。 Can anyone help me. 谁能帮我。

First Delete changeDetection: ChangeDetectionStrategy.OnPush 首先删除changeDetection: ChangeDetectionStrategy.OnPush

Secondly, you have: 其次,您有:
public Pages:Object; => avoid using a First capital letter for variables, so use public pages:Object; =>避免对变量使用首字母大写,因此请使用public pages:Object; instead. 代替。

then here you have: 那么你在这里:

 Pages => {
    this.Pages = Pages.data;
}

try: 尝试:

response => {
    this.pages = response.data;
}

I think you need to provide initial value. 我认为您需要提供初始价值。

for example - 例如 -

public Pages = null;

for more information read this article - Everything you need to know about the ExpressionChangedAfterItHasBeenCheckedError error . 有关更多信息,请阅读本文- 您需要了解有关ExpressionChangedAfterItHasBeenCheckedError错误的所有信息 It explain this error in great detail. 它详细解释了此错误。 Hope it helps. 希望能帮助到你。

暂无
暂无

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

相关问题 更改动态颜色 - ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改 - change dynamic color - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked ngu-carousel :: :: ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改 - ngu-carousel :: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 共享服务:ExpressionChangedAfterItHasBeenCheckedError:检查表达式后,表达式已更改 - Shared service: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked 角度4错误:检查后表达式已更改 - Angular 4 Error: Expression has changed after it was checked Angular - 表达式在检查后发生变化 - Angular - Expression Changed After It Has Been Checked Angular Expression 被检查后发生了变化 - Angular Expression has changed after it was checked 检查后表达式已更改-角度 - Expression has changed after it was checked - angular Angular2 - 表达式在检查后已更改 - 使用调整大小事件绑定到 div 宽度 - Angular2 - Expression has changed after it was checked - Binding to div width with resize events ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 以前的值:'ngIf:true'。 当前值:'ngIf:false' - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false' ExpressionChangedAfterItHasBeenCheckedError:从ckEditor获取值时检查后表达式已更改 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked while getting the value from ckEditor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM