admin管理员组文章数量:1323730
I am using Angular Material in my Angular 4 app. When I try to use the MatSnackBar
in the ngAfterViewInit()
, I am facing an error as:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'visible-bottom'.It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
I have used the ChangeDetectorRef
to detect the changes, but it doesn't seem to work.
Here's the code I have been working on:
constructor(private matSnackBar: MatSnackBar, private router: Router, private cdr: ChangeDetectorRef) { }
ngOnInit() {}
ngAfterViewInit() {
let snackBarRef = this.matSnackBar.open('Redirecting to dashboard..', 'Cancel', {
duration: 10000
});
snackBarRef.onAction().subscribe(() => {
console.log("Cancelled");
});
this.cdr.detectChanges();
}
Please help me resolve this issue.
I am using Angular Material in my Angular 4 app. When I try to use the MatSnackBar
in the ngAfterViewInit()
, I am facing an error as:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: 'visible-bottom'.It seems like the view has been created after its parent and its children have been dirty checked. Has it been created in a change detection hook ?
I have used the ChangeDetectorRef
to detect the changes, but it doesn't seem to work.
Here's the code I have been working on:
constructor(private matSnackBar: MatSnackBar, private router: Router, private cdr: ChangeDetectorRef) { }
ngOnInit() {}
ngAfterViewInit() {
let snackBarRef = this.matSnackBar.open('Redirecting to dashboard..', 'Cancel', {
duration: 10000
});
snackBarRef.onAction().subscribe(() => {
console.log("Cancelled");
});
this.cdr.detectChanges();
}
Please help me resolve this issue.
Share Improve this question edited Nov 28, 2017 at 11:33 starlight asked Nov 28, 2017 at 11:29 starlightstarlight 7854 gold badges16 silver badges31 bronze badges 3-
Did you try moving
this.cdr.detectChanges();
inside theonAction().subscribe(() => {});
– Amit Chigadani Commented Nov 28, 2017 at 12:05 - @AmitChigadani That doesn't work! – starlight Commented Nov 28, 2017 at 12:08
- Try to create a working plunker to replicate the error. Provided code won't help much. – Amit Chigadani Commented Nov 28, 2017 at 12:16
2 Answers
Reset to default 4One potential but crude solution is to use setTimeout -
let snackBarRef;
setTimeout(() => {
snackBarRef = this.matSnackBar.open('Redirecting to dashboard..', 'Cancel', {
duration: 10000
});
});
please refer https://github./angular/material2/issues/6158 for more information.
Try to move snackbar in your constructor:
constructor(private matSnackBar: MatSnackBar, private router: Router, private cdr: ChangeDetectorRef) {
let snackBarRef = this.matSnackBar.open('Redirecting to dashboard..', 'Cancel', {
duration: 10000
});
snackBarRef.onAction().subscribe(() => {
console.log("Cancelled");
});
}
本文标签: javascriptAngular 4 Error Expression has changed after it was checkedStack Overflow
版权声明:本文标题:javascript - Angular 4 Error: Expression has changed after it was checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742121361a2421718.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论