我在Angular中有一个反应形式,如下所示:

this.AddCustomerForm = this.formBuilder.group({
    Firstname: ['', Validators.required],
    Lastname: ['', Validators.required],
    Email: ['', Validators.required, Validators.pattern(this.EMAIL_REGEX)],
    Picture: [''],
    Username: ['', Validators.required],
    Password: ['', Validators.required],
    Address: ['', Validators.required],
    Postcode: ['', Validators.required],
    City: ['', Validators.required],
    Country: ['', Validators.required]
});

createCustomer(currentCustomer: Customer)
{
    if (!this.AddCustomerForm.valid)
    {
        //some app logic
    }
}

this.AddCustomerForm.valid返回false,但是一切看起来不错。

我试图通过检查控件集合中的status属性来查找。但是我想知道是否有一种方法可以找到无效的并显示给用户?

最佳答案

您可以简单地遍历每个控件并检查状态:

public findInvalidControls() {
    const invalid = [];
    const controls = this.AddCustomerForm.controls;
    for (const name in controls) {
        if (controls[name].invalid) {
            invalid.push(name);
        }
    }
    return invalid;
}

关于angular - 如何找到 Angular 4 react 形式的无效控件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45220073/

10-11 14:11