O
O
OlyaSoz2019-04-12 14:45:40
JavaScript
OlyaSoz, 2019-04-12 14:45:40

How can the ngIf condition be shortened if only one property in the condition changes?

There is such a condition, but it is very large and only one property is always repeated: firstName

<span *ngIf="registrationForm.controls.firstName.errors && registrationForm.controls.firstName.errors.required && registrationForm.controls.firstName.touched" class="clr-subtext">This field is required</span>

This piece is similar to the piece above, but with a different property: email
<span *ngIf="registrationForm.controls.email.errors && registrationForm.controls.email.errors.required && registrationForm.controls.email.touched" class="clr-subtext">This field is required</span>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YamalievDR, 2019-04-12
@OlyaSoz

isFieldRequired(field: string): boolean {
    const control = this.registrationForm.controls[field];
    return control.errors && control.errors.required && control.touched;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question