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

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

相关内容

热门资讯

透视经验!hhpkoer辅助挂... 透视经验!hhpkoer辅助挂是真的吗(德普之星透视)其实真的是有辅助方法(哔哩哔哩)在进入hhpk...
透视大纲!pokemmo辅助脚... 透视大纲!pokemmo辅助脚本(透视)哈糖大菠萝怎么开挂(辅助)竟然有方法(哔哩哔哩)1)哈糖大菠...
透视手筋!pokemmo手机版... 透视手筋!pokemmo手机版脚本(透视)大菠萝789辅助器下载(辅助)总是是有神器(哔哩哔哩)1)...
透视资料!约局吧德州真的有透视... 透视资料!约局吧德州真的有透视挂吗(透视)智星菠萝透视(辅助)都是真的是有app(哔哩哔哩)1、很好...
透视方式!xpoker辅助控制... 透视方式!xpoker辅助控制(WePoKer简单)真是真的有辅助软件(哔哩哔哩)1、每一步都需要思...
透视法门!wepoker轻量版... 透视法门!wepoker轻量版有透视吗(WPK安卓)果然是有辅助技巧(哔哩哔哩)1、让任何用户在无需...
透视练习!大菠萝辅助器(透视)... 透视练习!大菠萝辅助器(透视)拱趴游戏破解器(辅助)一贯有神器(哔哩哔哩)1)拱趴游戏破解器有没有挂...
透视绝活!poker mast... 透视绝活!poker master辅助(透视)菠萝德州透视脚本(辅助)确实一直都是有工具(哔哩哔哩)...
透视练习!wpk德州局怎么透视... 透视练习!wpk德州局怎么透视(德普之星私人局)其实真的是有辅助脚本(哔哩哔哩)1、该软件可以轻松地...
透视指南书!hhpoker买挂... 透视指南书!hhpoker买挂(WePoKer代打)好像真的是有辅助脚本(哔哩哔哩)1、该软件可以轻...