“如何修复角度为'index 0 in get error'的错误”

时间:2019-03-23 22:25:47

标签: angular7

我在angular-7下开发了一个带有输入验证的表格。输入中的所有项目都可以正常工作,只有一项始终显示值为0的索引。

我已经在互联网上搜索了建议的解决方案。

类型脚本:

constructor(private router: Router,
private service: BvUserService,
private fb: FormBuilder,
private toastr: ToastrManager,
private apiErrors: ApiErrorsService
) {
this.createForm = this.fb.group({
  'Login': ['', Validators.required],
  'OldPassWord': ['',Validators.required],
  'Password': ['', [Validators.required, Validators.minLength(8), Validators.maxLength(12)]],

});
this.apiErrors.onError.subscribe(_ => { console.log('get error'); this.setErrors(); });}
setErrors() {
let error = this.apiErrors.getErrors(location.pathname);

// Pour chaque controle du formulaire
Object.keys(this.createForm.controls).forEach(key => {
  const control = this.createForm.get(key);
  // Suppression des erreurs de validation
  control.setErrors(null);
  // Obtention des A©ventuelles erreurs de validation
  let errors = this.apiErrors.getFieldErrors(location.pathname, key);
  if (errors && errors.length > 0) { 
    // Application des erreurs sur le controle
    errors.forEach((value, idx) => {
      control.setErrors({ [`${key}-${idx}`]: value }, { emitEvent: true });
    });
  }

  control.markAsTouched();
});

html:

 <mat-form-field class="example-full-width">
      <input matInput placeholder="Entrez votre ancien mot de passe" [type]="hide ? 'password' : 'text'" formControlName="OldPassWord" required>
      <mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility_off' : 'visibility'}}</mat-icon>
      <mat-error *ngFor="let error of getErrors('OldPassWord')">
        {{error}}
      </mat-error>
    </mat-form-field>
  </p>

错误:

 {"key":"OldPassword","messages":["champs obligatoire"]}]}]
 api-errors.service.ts:68 index 0 get error

0 个答案:

没有答案