Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 检查角度反应形式的错误计数_Angular_Templates_Error Handling_Angular Forms - Fatal编程技术网

Angular 检查角度反应形式的错误计数

Angular 检查角度反应形式的错误计数,angular,templates,error-handling,angular-forms,Angular,Templates,Error Handling,Angular Forms,如果表单中的错误计数大于1,我希望有条件地应用css类。在angular4我该怎么做 组成部分: import { Component } from "@angular/core"; import { FormGroup, ReactiveFormsModule, FormBuilder, Validators } from '@angular/forms'; @Component({ ... }) export class RegisterComponent { complex

如果表单中的错误计数大于1,我希望有条件地应用css类。在angular4我该怎么做

组成部分:

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

@Component({
    ...
})

export class RegisterComponent {
  complexForm : FormGroup;

  constructor(fb: FormBuilder){
    this.complexForm = fb.group({
      'emailAddress' : [null, Validators.email],
      'firstName': [null, Validators.compose([Validators.required, Validators.minLength(2)])],
      ...
    })
  }

  submitForm(value: any){
    console.log(value);
  }
}
模板:

<form [formGroup]="complexForm" (ngSubmit)="submitForm(complexForm.value)">
      <section class="form-block">
          <div class="form-group" [ngClass]="{'has-error':!complexForm.controls['emailAddress'].valid && complexForm.controls['emailAddress'].touched}">
                <label for="formFields_1">Email Address</label>
                <input [formControl]="complexForm.controls['emailAddress']" type="text" spellcheck="false" id="formFields_1" placeholder="" size="35">
                <span *ngIf="complexForm.controls['emailAddress'].hasError('email') && complexForm.controls['emailAddress'].touched" class="tooltip-content">
                    Please enter a valid email address (ex. joan@vmware.com)
                </span>
          </div>   
          <div class="form-group" [ngClass]="{'has-error':!complexForm.controls['firstName'].valid && complexForm.controls['firstName'].touched}">
              <label for="formFields_2">First Name</label>
                <input [formControl]="complexForm.controls['firstName']" type="text" spellcheck="false" id="formFields_2" placeholder="" size="35">
                <span *ngIf="complexForm.controls['firstName'].hasError('required') && complexForm.controls['firstName'].touched" class="tooltip-content">
                    Your first name must contain at least one letter
                </span>
          </div>
     </section>
</form>

电子邮件地址
请输入有效的电子邮件地址(例如:。joan@vmware.com)
名字
您的名字必须至少包含一个字母

如果我想将类
表单错误
应用于
如果表单错误计数大于1,我该如何做?谢谢你的想法。

我不知道Angular能给你什么方法。您必须在组件类中编写自己的方法来计算此值。您需要将每个控件中的错误计数相加

大概是这样的:

getErrorCount(container: FormGroup): number {
    let errorCount = 0;
    for (let controlKey in container.controls) {
        if (container.controls.hasOwnProperty(controlKey)) {
            if (container.controls[controlKey].errors) {
                errorCount += Object.keys(container.controls[controlKey].errors).length;
                console.log(errorCount);
            }
        }
    }
    return errorCount;
}

我不知道Angular是怎么给你这个的。您必须在组件类中编写自己的方法来计算此值。您需要将每个控件中的错误计数相加

大概是这样的:

getErrorCount(container: FormGroup): number {
    let errorCount = 0;
    for (let controlKey in container.controls) {
        if (container.controls.hasOwnProperty(controlKey)) {
            if (container.controls[controlKey].errors) {
                errorCount += Object.keys(container.controls[controlKey].errors).length;
                console.log(errorCount);
            }
        }
    }
    return errorCount;
}

有一种更好的解决方法

Object.keys(formGroup.errors).length

这将向我们返回formGroup中的错误总数

有更好的解决方法

Object.keys(formGroup.errors).length

这将返回formGroup中的错误总数

这对我不起作用。这对我不起作用。