0

我有以下组件,它应该根据要求检查的代码以及传递给它的控件的条件显示错误消息:

import {
  Component,
  Input
} from '@angular/core';
import {
  AbstractControl
} from '@angular/forms';

export interface ErrorCodeMessage {
  code: string;
  message: string;
}

@Component({
  selector: 'app-field-errors',
  templateUrl: './field-errors.component.html',
  styleUrls: ['./field.errors.component.scss']
})

export class FieldErrorsComponent {
  @Input() controls: AbstractControl[];
  @Input() formSubmitted: boolean;
  @Input() errorsToTest: ErrorCodeMessage[];

  public controlsAreNotPristine(): boolean {
    let arePristine = false;
    for (let i = 0; i < this.controls.length; i++) {
      arePristine = arePristine && this.controls[i].pristine;
    }
    return !arePristine;
  }

  public controlsHaveError(code: string): boolean {
    let haveError;
    for (let i = 0; i < this.controls.length; i++) {
      haveError = haveError || this.controls[i].hasError(code);
    }
    return haveError;
  }
}
<ng-container *ngIf="controlsAreNotPristine() || formSubmitted">
  <div *ngFor="let error of errorsToTest" [hidden]="!controlsHaveError(error.code)" [translate]="error.message"></div>
</ng-container>

这显然是行不通的,因为变量arePristinehaveError被遮蔽,因为我正在使用let运算符。但是,TSLint 不想让我使用var!我将如何正确地做到这一点?

4

0 回答 0