[英]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:我尝试过的事情:
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.