【问题标题】:ion-button [disabled] not working unless another element is changed after reCAPTCHA validation除非在 reCAPTCHA 验证后更改另一个元素,否则 ion-button [disabled] 不起作用
【发布时间】:2021-10-13 14:31:30
【问题描述】:

似乎输入验证工作正常(即正则表达式、名称大小、选择),因为只要任何其他输入更新(并且有效)并且验证码在更改之前已完成,“提交”按钮就可以选择.然而,当验证码最后完成时,离子按钮仍然被禁用,尽管所有输入都有效,包括验证码。就好像 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;
  }

任何想法为什么会发生这种情况?

【问题讨论】:

    标签: html angular forms ionic-framework recaptcha


    【解决方案1】:

    尝试使用 ngZone:

    在构造函数中:public ngZone: NgZone

    别忘了import { NgZone } from '@angular/core';

    然后:

    getResponseCapcha(captchaResponse: string) {
        this.ngZone.run(() => this.captchaSuccess = true);
    }
    

    这将强制更新 UI。

    【讨论】:

    • 嘿!感谢您的评论。这行代码:getResponseCapcha(captchaResponse: string) { this.ngZone.run(() =&gt; this.captchaSuccess = true); } 在我的代码中产生“未捕获(承诺中)TypeError”。关于如何解决它的任何想法?
    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-15
    • 2011-12-11
    • 2015-04-18
    相关资源
    最近更新 更多