关于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 ...
随机推荐
- 逆向工程之修改关键CALL返回值_破解视频转换专家
1)注册软件随便输入注册名注册码 2)进入软件根目录,发送到PEID查壳 3)发现无壳 4)发送到OD 4.1)右键菜单选择智能搜索 4.2)找到关键信息点注册 4.3)找到关键信息点双击进入汇编,向 ...
- shop_z 一套非常适合二次开发的php后台管理系统
QQ群:247823727 如果你需要定制某些功能开联系群主,价格实惠,后期交接完善,有上手培训 shop_z基础thinkphp5 php7上开发运行,速度杠杠的 地址:https://gitee ...
- 通过阿里云命令行工具 aliyuncli 购买服务器
开始想通过 aliyuncli 的 golang 源码进行编译安装(注:python 版的 aliyuncli 已不再维护),但没成功,详见 通过 golang 源码编译阿里云命令行工具 aliyun ...
- xilink 烧写flash
no 右键
- VAE (variational autoencoder)
https://www.zhihu.com/question/41490383/answer/103006793 自编码是一种表示学习的技术,是deep learning的核心问题 让输入等于输出,取 ...
- Java之事务的基本应用
基本介绍 事务是数据一致性最基本的保证,也就是说一个事务中的操作要么都成功,要么都失败,不允许部分成功.我们常说的事务就是jdbc事务,当然Java中还有其他事务,并且在使用jdbc事务有很多注意点, ...
- SQL Server (MSSQLSERVER) 服务由于下列服务特定错误而终止: %%17051
问题出现:今天在给客户调试项目的时候,发现无法连接SQL server数据库 解决过程:1.在cmd命令窗口输入services.msc,打开服务窗口,找到SQL Server (MSSQLSERVE ...
- 20.0-uC/OS-III移植
1.CPU移植要求: 1) 处理器有对应的能产生可重入代码的C编译器2) 处理器支持中断且能提供周期性的中断(通常介于10到1000Hz之间). 3) 可以关中断和开中断4) 处理器支持存储和载入堆栈 ...
- html5中JavaScript删除全部节点
如果div里有这么些内容: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type ...
- ubuntu12下subversion 1.6升级为1.8版本
应用场景是.android源码体积太大.我从服务器上svn co过来,速度很慢.服务器是ubuntu14版本,我工作的机器是ubuntu12版本,14上面svn版本是1.8.8,12上svn的版本是1 ...