简体   繁体   English

Angular - ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改

[英]Angular - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked

I really can't understand this, I already try search in google, here, everywhere... and i can't understand why this error still showing.我真的无法理解这一点,我已经尝试在谷歌中搜索,在这里,无处不在......我不明白为什么这个错误仍然显示。

I have something like this:我有这样的事情:

parent.html父.html

<child-1 [(type)]="type"></child-1>

parent.ts父母.ts

type = 0;
getTypeToOtherthings() { // function to use in a mouse event, it's not important for that
    console.log(type);
}

child-1.html child-1.html

<child-2 [(type)]="type">

child-1.ts // this component is like a bridge between the parent and child-2 (maybe the problem is here, i really don't know) child-1.ts // 这个组件就像是 parent 和 child-2 之间的桥梁(可能问题出在这里,我真的不知道)

_type: number;
@Input() set type(val: number) {
  this.typeChange.emit(val);
  this._type= val;
}
get type() {
  return this._type;
}
@Output() typeChange: EventEmitter<number> = new EventEmitter<number>();

child-2.html child-2.html

(some HTML) (一些 HTML)

child-2.ts child-2.ts

_type: number;
@Input() set type(val: number) {
  this.typeChange.emit(val);
  this._type= val;
}
get type() {
  return this._type;
}
@Output() typeChange: EventEmitter<number> = new EventEmitter<number>();

/*I need change Type variable in childs-2, so i put this in 
onInit (but i already tryed put this in all Afters... , using Promise, ChangeDetectorRef, nothing works,
like everything works fine but the error still)*/
ngOnInit() {
    this.type = 2;
}

after all Im almost giving up毕竟我几乎要放弃了

Quick and dirty, but if you see this issue and know the problem line, you can run it in a setTimout, eg又快又脏,但是如果你看到这个问题并且知道问题所在,你可以在 setTimout 中运行它,例如

setTimeout(()=> this.markersEventsType = 2;)

But there's probably a more suitable solution.但可能有更合适的解决方案。 Does it not work to set the eventType in the constructor?在构造函数中设置 eventType 不起作用吗? If you're getting the change from a service, have you tried using the async pipe?如果您从服务中获得更改,您是否尝试过使用异步管道?

This indicates a problem in your app.这表明您的应用存在问题。 In short, you are modifying the model but not the view.简而言之,您正在修改模型而不是视图。 Angular's devMode adds update checks in addition to the ones already existing. Angular 的 devMode 除了已经存在的更新检查之外,还添加了更新检查。 Those checks look for any updates in the model, so that the view gets updated.这些检查会查找模型中的任何更新,以便更新视图。

You are getting this error because none of those checks managed to detect a change in the model.您收到此错误是因为这些检查均未设法检测到模型中的更改。 Therefore, one solution would be to use ChangeDetectorRef .因此,一种解决方案是使用ChangeDetectorRef Calling detectChanges() tells your app that something changed in the model and that the vue can be updated.调用detectChanges()告诉您的应用模型中的某些内容发生了变化并且可以更新 vue。

In addition to this answer, I strongly recommend you looking at change detection strategies which in my opinion makes the code more understandable (because it forces you to use ChangeDetection), and in a more general manner, your app will run smoother.除了这个答案,我强烈建议您查看更改检测策略,在我看来,它使代码更易于理解(因为它迫使您使用 ChangeDetection),并且以更一般的方式,您的应用程序将运行得更流畅。

暂无
暂无

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

相关问题 Angular 4:`ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后发生了变化 - Angular 4: `ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked ExpressionChangedAfterItHasBeenCheckedError:在角度检查表达式后,表达式已更改 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked in angular Angular 2+ ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改 - Angular 2+ ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked Angular4 / ngrx-ExpressionChangedAfterItHaHasBeenCheckedError:检查表达式后,表达式已更改 - Angular4/ngrx - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked Angular:错误错误:ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改 - Angular: ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked Angular ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 cdkdrag - Angular ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. cdkdrag Angular — ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 (嵌套的FormArray) - Angular — ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. (Nested FormArray) ExpressionChangedAfterItHasBeenCheckedError:使用Angular2检查表达式后,表达式已更改 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked using Angular2 Angular 中的“ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改”错误 - "ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked" error in Angular ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Angular - ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM