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中构建了一个表单,并在其状态进行更改时订阅了状态更改事件。 如果表单有效,则将表单启用。

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

相关内容

热门资讯

透视好牌!we poker游戏... 透视好牌!we poker游戏下,wejoker辅助脚本(透视)必备教程(有挂技巧)进入游戏-大厅左...
透视辅助!wepoker透视挂... 透视辅助!wepoker透视挂底牌,hhpoker德州真的假的,揭秘教程(有挂秘笈)1、上手简单,内...
透视攻略!wepoker私人局... 透视攻略!wepoker私人局透视方法,wepokerplus外挂(透视)揭秘攻略(有挂教程)透视攻...
透视好友房!拱趴大菠萝十三水作... 透视好友房!拱趴大菠萝十三水作弊,aapoker透视方法,新2025版(有挂技巧)拱趴大菠萝十三水作...
透视能赢!wepoker有透视... 透视能赢!wepoker有透视功能吗,wepoker如何设置透视,介绍教程(有挂技巧)1、玩家可以在...
透视美元局!wepoker破解... 透视美元局!wepoker破解器有用吗,wepoker有用吗(透视)系统教程(有挂教程);小薇(透视...
透视模拟器!hhpoker是真... 透视模拟器!hhpoker是真的还是假的,wepoker辅助透视软件,AI教程(有挂规律);一、hh...
透视好友房!wepoker作弊... 透视好友房!wepoker作弊方法,wepoker有没有插件(透视)AI教程(有挂教程)1、wepo...
透视黑科技!wepoker透视... 透视黑科技!wepoker透视脚本免费,wepoker私人局透视教程,2025新版教程(有挂内幕)1...
透视透视!wepoker模拟器... 透视透视!wepoker模拟器哪个好用,wepoker轻量版透视方法(透视)普及教程(有挂介绍)1)...