检查formgroupname中的任何控件是否有错误

问题描述 投票:0回答:1

我在组中有这些控件,我希望一旦发生错误,它就会在模板中显示出来。

我的文件.ts:

  //... Some stuff
  export class FormularioFacturaComponent implements OnInit { 

    // .... Some stuff
    private pcIVA = new FormControl('', [
        Validators.required
    ]);
    createForm() {
        this.formulario = this.formBuilder.group({  
            facturasBN: this.formBuilder.group({
                lectura: new FormControl('0', [Validators.required]),
                copias: new FormControl('0', [Validators.required]),
                descuento: new FormControl('0', [Validators.required])
            }),       
            pcIVA: this.pcIVA,
        });
    }
    // .... Some stuff
}

我的file.html:

    <form [formGroup]="formulario" #form="ngForm">
        <div class="content">       
          <div class="form-group" formGroupName="facturasBN">
                <div class="control">
                    <label for="lecturasBNLectura">Lectura</label>
                    <input type="text" class="form-control number" #facturasBNlectura formControlName="lectura" currencyFormatterDirective [pressPointDecimal]="false" [setFormat]="true" [setDecimals]="0" />
                </div>
                <div class="control">
                    <label for="lecturasBNCopias">Copias</label>
                    <input type="text" class="form-control number" #facturasBNCopias formControlName="copias" currencyFormatterDirective [pressPointDecimal]="false" [setFormat]="true" [setDecimals]="0" />
                </div>
                <div class="control">
                    <label for="lecturasBNDesviacion">Descuento</label>
                    <input type="text" class="form-control number" #facturasBNDesviacion formControlName="descuento" currencyFormatterDirective [pressPointDecimal]="false" [setFormat]="true" [setDecimals]="0" />
                </div>
             </div>          
             <!-- Here I want to ask if there was an error in any control of formGroupName -->
             <app-field-error-display [displayError]="formulario.controls['facturasBN'].errors" errorMsg="the fields marked in red are obligatory"></app-field-error-display>

            <div class="control">
                <label>% IVA</label>
                <input type="text" class="form-control number" #lecturasPCIVA formControlName="pcIVA" currencyFormatterDirective [pressPointDecimal]="true" [setFormat]="true">
             </div>          
            <button type="submit" [disabled]="!formulario.valid" class="btn btn-primary boton" style="float:right;" (click)="onClick()">
                <span class="glyphicon glyphicon-floppy-disk"></span> Guardar
            </button>
         </div>      
    </form>

当我询问formGroupName中是否有错误时:

formulario.controls['facturasBN'].errors"

错误未显示

任何的想法?

angular reactive-forms
1个回答
0
投票

我这样做的方式是formulario.get('facturasBN').hasError('required'),但你并不是真的对特定的错误感兴趣,所以你也可以做formulario.get('facturasBN').errors

显然更新我没有很好地阅读你的问题。我上面给出的答案是单个控件而不是组。

© www.soinside.com 2019 - 2024. All rights reserved.