Angular 2 Modal Popup Error “Expression has changed after it was checked”

烈酒焚心 提交于 2019-12-05 10:47:09

Seems your error occurs after execution following code:

ngAfterViewInit() {
    if (!this._elRef.nativeElement.contains(document.activeElement)) {
      this._renderer.invokeElementMethod(this._elRef.nativeElement, 'focus', []);
    }
}

https://github.com/ng-bootstrap/ng-bootstrap/blob/1.0.0-alpha.20/src/modal/modal-window.ts#L65

on input is fired blur event that marks your control as touched.

It doesn't work for AccountComponent because detection changes in AccountComponent occurs before ngbModalContainer while FormGroup within app.component.html gets right values.

Possible solutions:

1) mark your controls as touched before opening modal

account.component.ts

onUpdatePassword() {
  Object.keys(this.changePasswordForm.controls).forEach(key => {
     this.changePasswordForm.controls[key].markAsTouched();
  });

  this.alertService.alertPopup('test2', 'asfafa')
}

2) change order tags

app.component.html

<template ngbModalContainer></template>
<router-outlet> </router-outlet>

I even gone through same error once,

Use ngAfterViewInit() in your appComponent instead ngOnInit.

If that didn't solve just try

setTimeOut(function(){
//Your Code
},1)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!