html - 除非在 reCAPTCHA 验证后更改另一个元素,否则 ion-button [disabled] 不起作用
问题描述
似乎输入验证工作正常(即正则表达式、名称大小、选择),因为只要任何其他输入更新(并且有效)并且验证码在更改之前已完成,就可以选择“提交”按钮。然而,当验证码最后完成时,离子按钮仍然被禁用,尽管所有输入都有效,包括验证码。就好像 ion-button [disable] 属性在 recaptcha 成功时不会刷新,并且仅在更新任何其他输入时才会刷新。
HTML:
<form #testForm="ngForm" (ngSubmit)="submitForm()">
<ion-radio-group value="{{selectTest}}" [(ngModel)]="selectTest" name="selectTest" mode="md">
<ion-radio value="Test1" (click)="selectTest='test1'"></ion-radio>
<ion-label>Test1</ion-label>
<ion-radio value="Test2" (click)="selectTest='test1'"></ion-radio>
<ion-label>Test2</ion-label><br>
</ion-radio-group>
<ion-item class="cust_input" lines="none">
<ion-input
[(ngModel)]="firstName"
name="firstName">
</ion-input>
</ion-item>
<ion-item class="input" lines="none">
<ion-label position="floating" class="required">Email</ion-label>
<ion-input
[(ngModel)]="email"
placeholder="ex: email@yahoo.com"
name="email">
</ion-input>
</ion-item>
<div id="capcha_element" class="g-recaptcha" data-callback="getResponseCapcha" data-expired-callback="getExpiredResponse" [attr.data-sitekey]="siteKey"></div>
<ion-button
mode="ios"
type="submit"
[disabled]="firstName.trim().length<1
|| !emailRegex.test(email)
|| selectTest==''
|| !captchaSuccess"
>
Submit
</ion-button>
</form>
TS:
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class TestPage{
constructor(
public captchaSuccess: boolean = false;
)
ngOnInit() {
this.contactForm = this.formBuilder.group({
recaptcha: ['', Validators.required]
});
grecaptcha.render('capcha_element', {
'sitekey': this.siteKey,
});
window['getResponseCapcha'] = this.getResponseCapcha.bind(this);
window['getExpiredResponse'] = this.getExpiredResponse.bind(this);
}
getResponseCapcha(captchaResponse: string) {
this.captchaSuccess = true;
}
getExpiredResponse() {
this.captchaSuccess = false;
}
任何想法为什么会发生这种情况?
解决方案
尝试使用 ngZone:
在构造函数中:public ngZone: NgZone
不要忘记import { NgZone } from '@angular/core';
然后:
getResponseCapcha(captchaResponse: string) {
this.ngZone.run(() => this.captchaSuccess = true);
}
这将强制在 UI 上进行更新。
推荐阅读
- excel - 使用后期绑定从 Access vba 中删除 Row.delete
- html - 为什么 col-xs-0 占用 100% 的行宽?
- python - Python - 如何改进编织功能并将功能添加到类
- javascript - 无法访问 Firebase 集合中的特定字段
- linux - 使用 sed 更改 crontab 行的标准输出
- jsp - Wildfly 12 上的会话属性丢失
- java - 让骆驼使用 Map 构建端点 uri
- javascript - 如何使用 React for Switch Buttons 从 React-Bootstrap 在地图中设置 onchange?
- bash - 如何让 cronjob 在重新启动时删除 RHEL 服务器上的一些 pid 文件?
- kubernetes - ArgoCD 漂移检测