关于ExpressionChangedAfterItHasBeenCheckedError
最近在stackoverflow上似乎每天都有一些关于angular报错‘ExpressionChangedAfterItHasBeenCheckedError’的问题。发生这些问题通常是由于angular的开发者不懂angular变更检测的工作原理,以及为什么这个检测的报错是有必要的。很多开发者甚至认为这是angular的bug。但其实不是的。这是一个用于防止模型数据和ui之间数组不一致的一个警告机制,以便不让用户在页面上看到陈旧的或者错误的数据。
@Component({
selector: 'a-comp',
template: `
{{name}}
`
})
export class AComponent {
name = 'I am A component';
text = 'A message for the child component`;
view.oldValues[0] = 'A message for the child component';
view.oldValues[1] = 'I am A component';
AComponentView.instance.text === view.oldValues[0]; // false
'A message for the child component' === 'updated text'; // false
AComponentView.instance.name === view.oldValues[1]; // false
'I am A component' === 'updated name'; // false
export class BComponent {
@Input() text;
constructor(private parent: AppComponent) {}
ngOnInit() {
this.parent.text = 'updated text';
}
}
export class BComponent {
@Input() text;
constructor(private parent: AppComponent) {}
ngAfterViewInit() {
this.parent.name = 'updated name';
}
}
不禁思考,它是在变化检测勾子中创建的吗?
通常,修复方案即通过正确的变更检测机制来创建动态组件。例如上面章节中的最后一个例子,可以将动态组件的创建过程移到ngOnInit生命周期勾子中,尽管文档说明ViewChild只能在ngAfterViewInit之后使用,但是在创建视图的时候,它归属于子组件,因此可以更早使用。
export class BComponent {
name = 'I am B component';
@Input() text;
constructor(private parent: AppComponent) {}
ngOnInit() {
setTimeout(() => {
this.parent.text = 'updated text';
});
}
ngAfterViewInit() {
setTimeout(() => {
this.parent.name = 'updated name';
});
}
}
如果你在使用EventEmitter,你可以传递true参数选项来设置异步机制
export class AppComponent {
name = 'I am A component';
text = 'A message for the child component';
constructor(private cd: ChangeDetectorRef) {
}
ngAfterViewInit() {
this.cd.detectChanges();
}
}
译自:Everything you need to know about the `ExpressionChangedAfterItHasBeenCheckedError` error
关于ExpressionChangedAfterItHasBeenCheckedError的更多相关文章
- ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. 解决方案: 异步更新 ...
- ExpressionChangedAfterItHasBeenCheckedError详细解释
一个angular组件,他的生命周期是这样的 update bound properties for all child components/directives call ngOnInit, On ...
- 初入angular4——实际项目搭建总结
前言 接到一个pc端后台项目,还会加入两个安卓同事一起学习和做这个项目,需要带一下他们. 既ng1之后,我就没怎么有过其它后台框架的实际项目经验了,期间用的移动端框架也并非vue.angular系列. ...
- angular学习第1步
#### 最专业,最全面的angular的学习文档 https://www.jianshu.com/p/f0f81a63cbcb ### https://www.cnblogs.com/xiaowei ...
- cuowu
ngFor不能用于Object rowspan colspan不能绑定变量,要用attr.colspan https://stackoverflow.com/questions/35615751/wh ...
- Angular-cli 构建应用的一些配置
Angular-cli 构建应用 的一些配置 标签(空格分隔): Angular 直接使用 ng build --prod --build-optimizer --base-href=/ 来发布 ba ...
- Angular2的双向数据绑定
什么是双向绑定 如图: 双向绑定.jpg 双向绑定机制维护了页面(View)与数据(Data)的一致性.如今,MVVM已经是前段流行框架必不可少的一部分. Angular2中的双向绑定 双向绑定, ...
- Angular变更检测策略报错
报错信息: ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was ...
随机推荐
- liunx系统问题总结
1.Unable to locate package错误 解决办法 :输入命令 sudo apt-get update,进行软件的更新
- MacOS High Sierra 引起 VirtualBox Vagrant 同步慢
问题 最近把mac的操作系统升级到了最新版本发现了一个问题,通过共享文件夹的方式 修改的文件,无法立即同步到虚拟机中,大概需要30秒才能同步到共享文件夹. 操作环境如下 虚拟机:Virtualbox ...
- Java+selenium如何清理浏览器Cookie
一.场景:在未注销系统切换不同账号登录系统下,登录会有浏览器缓存,导致登录时间异常缓慢.跟开发浏览器缓存处理机制有关系. 二.解决方法: 获取浏览器Cookie,在Login方法前增加清除缓存的代码. ...
- Redis入门到高可用(十九)——Redis Sentinel
一.Redis Sentinel架构 二.redis sentinel安装与配置 四.客户端连接Sentinel 四.实现原理—— 故障转移演练(客户端高可用) 五.实 ...
- spring datasource 使用 proxool
XmlWebApplicationContext使用的xml配置如下: <?xml version="1.0" encoding="UTF-8"?> ...
- int bool 字符串 列表 字典 集合
1.int和bool 输出i的最大二进制位数inti = 1000 print(i.bit_length()) 2. str int bool list set dict tuple 相互转换 pr ...
- pandas处理时间序列(2):DatetimeIndex、索引和选择、含有重复索引的时间序列、日期范围与频率和移位、时间区间和区间算术
一.时间序列基础 1. 时间戳索引DatetimeIndex 生成20个DatetimeIndex from datetime import datetime dates = pd.date_rang ...
- cookie session 讲解
cookie: cookie的定义: cookie 是由web服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息,并且在每次请求时会携带保存的数据去访问服务器,所以cookie有 ...
- 使用 Docker 搭建 Java Web 运行环境(转)
原文 http://www.importnew.com/21798.html Docker 是 2014 年最为火爆的技术之一,几乎所有的程序员都听说过它.Docker 是一种“轻量级”容器技术,它几 ...
- 关于table的td和ul元素li隔行变色的功能实现
table元素的td和ul元素li隔行变色的功能实现 利用css控制二者的样式轻松实现隔行换色: 例如:table的css样式控制: table tr td{ background-color:颜 ...