Angular响应式表单控件都报告为ng-valid,父表单报告为ng-valid,但提交仍然为禁用状态,直到单击一个表单控件
创始人
2024-10-30 06:31:10
0

这通常发生在表单第一次呈现时,因为表单中的所有控件都是初始的ng-valid状态。要解决此问题,我们可以在ngOnInit钩子中检查表单的有效性并更新其状态。

示例代码:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-example-form',
  templateUrl: './example-form.component.html',
  styleUrls: ['./example-form.component.css']
})
export class ExampleFormComponent implements OnInit {

  form: FormGroup;

  constructor(private fb: FormBuilder) { }

  ngOnInit() {
    this.form = this.fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', Validators.required],
      phone: ['', Validators.required]
    });

    this.form.statusChanges.subscribe(status => {
      if (status === 'VALID') {
        this.form.enable(); // enable the form
      }
    });
  }

  submit() {
    console.log('Form submitted!');
  }

}

在这个示例中,我们在ngOnInit中构建了一个表单,并在其状态进行更改时订阅了状态更改事件。 如果表单有效,则将表单启用。

这个解决方法会使表单一旦所有控件都是有效的,就会启用提交按钮。

相关内容

热门资讯

透视线上!wpk透视辅助方法,... 透视线上!wpk透视辅助方法,约局吧开挂(详细辅助插件教程)1、让任何用户在无需wpk透视辅助方法A...
aapoker外挂!aapok... aapoker外挂!aapoker辅助工具下载(透视)原先是真的有挂(详细辅助AA德州教程)1、在a...
透视辅助!aapoker辅助挂... 透视辅助!aapoker辅助挂,wepoker免费脚本弱密码(详细辅助德州教程)1、aapoker辅...
微扑克ai机器人!来玩德州挂辅... 微扑克ai机器人!来玩德州挂辅助器(透视)好像是有挂(详细辅助黑科技教程);人气非常高,ai更新快且...
wepoke的确有挂!轰趴大菠... 自定义wepoke的确有挂系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,...
透视安卓版!wepoker辅助... 透视安卓版!wepoker辅助器怎么用,wepoker插件辅助(详细辅助解密教程)1、玩家可以在we...
wpk有透视辅助!AA POK... 1、wpk有透视辅助!AA POKER下载软件(透视)切实有挂(详细辅助安装教程);代表性(透视辅助...
透视脚本!aapoker透视怎... 透视脚本!aapoker透视怎么用,wpk可以作弊吗(详细辅助2025版教程);运aapoker透视...
aapoker有猫腻!德扑之星... aapoker有猫腻!德扑之星专业代打(透视)起初真的有挂(详细辅助扑克教程)1、aapoker有猫...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本免费下载,hhpoker有没有外挂(详细辅助科技教程)1、实时wep...