简体   繁体   English

使用单选按钮组打开 Angular 模态 window 会导致 ExpressionChangedAfterItHasBeenCheckedError

[英]Opening an Angular modal window with a Radio Button group causes ExpressionChangedAfterItHasBeenCheckedError

I am new to Angular and have tried to follow the lifecycle hooks, but apparently, I do not completely understand them.我是 Angular 的新手,并尝试遵循生命周期挂钩,但显然,我并不完全理解它们。

In my project, I have a Radio Button Group contained inside a modal window.在我的项目中,我在模态 window 中包含一个单选按钮组。 The modal is opened via button click at the user request, so nothing automatic.模态是通过在用户请求下单击按钮打开的,所以没有什么是自动的。 The monthly option needs to be selected by default when the window is displayed.显示 window 时需要默认选择每月选项。

When the modal window is opened, I get the following error in my dev tool console:打开模态 window 时,我在开发工具控制台中收到以下错误:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: 'focus: undefined'.以前的值:'焦点:未定义'。 Current value: 'focus: true'.当前值:'焦点:真'。

Things that I have tried:我尝试过的事情:

  • On modal open, manually setting the radio group to blur.在模态打开时,手动将无线电组设置为模糊。
  • Before modal open, manually setting the radio group to blur.在模态打开之前,手动将单选组设置为模糊。
  • Before and after the modal opening using ChangeDetectorRef to update any changes.在模态打开之前和之后使用 ChangeDetectorRef 更新任何更改。

At this point, I am stumped and would appreciate some guidance.在这一点上,我很困惑,希望得到一些指导。

HTML of the modal with radio button group:带有单选按钮组的模态的 HTML:

  <ng-template #subscriptionOptions let-modal>
    <form #subForm='ngForm'>
    <div class="modal-body" id="subInfo">
      <section class="widget">

          <h4>
            <fa-icon [icon]="['far','check-square']"></fa-icon>
            {{ currentUser.firstName }}, please choose and activate your subscription
          </h4>

        <div name="subType" id="subType" class="btn-group btn-group-toggle" ngbRadioGroup [(ngModel)]="subType" style="margin-left: 117px">
          <label class="BG-Def" ngbButtonLabel>
            <input name="monthly" id="monthly" type="radio" [value]="'monthly'" ngbButton  (click)="subType = 'monthly'">
            Pay Monthly
          </label>
          <label class="BG-Def" ngbButtonLabel>
            <input name="annual" id="annual" type="radio" [value]="'annual'" ngbButton  (click)="subType = 'annual'">
            Pay Annualy (Save 20%)
          </label>
        </div>


              <div class="row">
                <div class="col-md-8 col-md-offset-3">
                  <button type="submit" class="btn btn-danger">Activate Subscription</button>
                  <button type="button" class="btn btn-default" (click)="closeSubOptions()">Cancel</button>
                </div>
              </div>



      </section>
    </div>
    </form>
  </ng-template>

I am setting the subType value inside of my constructor:我在构造函数中设置 subType 值:

constructor(private modalDialogService: ModalDialogService) {
this.subType = 'monthly';
}

Then here is how I am opening the modal itself:然后这是我打开模态本身的方式:

HTML Button: HTML 按钮:

(click)="this.openSubOptions(subscriptionOptions)"

Typescript: Typescript:

  public openSubOptions(modal) {
      this.modalService.open(modal, {size: 'lg', windowClass: 'subModal'});
  }

ngOnInit and ngAfterViewInit ngOnInit 和 ngAfterViewInit

public ngOnInit() {
    this.passChange = false;
    this.navBar.updateUser.subscribe((result) => {
      this.currentUser = this.auth.getLoggedInUser();
    });
    this.currentUser = this.auth.getLoggedInUser();
    this.getSessionExpiration();
    this.timer = setInterval(() => {
      this.onTimeoutTick();
    }, 1000);
    this.dialogCallback.subscribe(() => this.resetTimer());
  }

  public ngAfterViewInit() {
    this.cd.detectChanges();
  } 

I ended up finding a work around the draws focus away from my Radio Group.我最终找到了一项工作,围绕抽奖焦点远离我的广播组。

I added an invisible input before my radio group that seems to prevent the change error from occurring.我在我的广播组之前添加了一个不可见的输入,这似乎可以防止发生更改错误。

<input type="text" style="display:none" />

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

相关问题 在angular 4中打开引导程序模态时获取ExpressionChangedAfterItHasBeenCheckedError - Getting ExpressionChangedAfterItHasBeenCheckedError while opening bootstrap modal in angular 4 在打开角度模态弹出窗口时未选中角度材料单选按钮 - Angular material radio button unchecked on opening angular modal pop up ExpressionChangedAfterItHasBeenCheckedError 由于单选按钮 - ExpressionChangedAfterItHasBeenCheckedError due to the radio button Angular - Modal NgBootstrap ExpressionChangedAfterItHasBeenCheckedError - Angular - Modal NgBootstrap ExpressionChangedAfterItHasBeenCheckedError 角度加载按钮ExpressionChangedAfterItHaHasBeenCheckedError - Angular loading button ExpressionChangedAfterItHasBeenCheckedError Angular Material Modal 导致窗口向下滚动 - Angular Material Modal causes window to scroll down 角度对话框未作为模态窗口打开 - Angular dialog not opening up as a modal window Angular 8 ExpressionChangedAfterItHasBeenCheckedError 反应形式的无线电验证 - Angular 8 ExpressionChangedAfterItHasBeenCheckedError radio validation on reactive form Angular 12 ngFor 指令中的 ViewChildren 导致 ExpressionChangedAfterItHasBeenCheckedError - Angular 12 ViewChildren in ngFor directive causes ExpressionChangedAfterItHasBeenCheckedError Angular5 Selectbox 打开一个 Modal -&gt; ExpressionChangedAfterItHasBeenCheckedError - Angular5 Selectbox open a Modal -> ExpressionChangedAfterItHasBeenCheckedError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM