Skip to content

bug(mat-form-field): Nesting ngIfs or @if inside mat-form-field causes mat-error to be displayed weirdly #30724

@lemonsnaw

Description

@lemonsnaw

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

If you have mat-error deep nested in @if or ngifs , it is not displayed correctly .

<-- doesn't work -->
<mat-form-field class="example-full-width">
  <mat-label>Email</mat-label>
  <input
    type="email"
    matInput
    [formControl]="emailFormControl"
    placeholder="Ex. pat@example.com"
  />
  @let somecondition=true;
  @if(somecondition)
  {
    @if (emailFormControl.hasError('required')) {
      <mat-error>Email is <strong>required</strong></mat-error>
    }
  }
  
</mat-form-field>

refer to 2nd formcontrol.
Only different between them is one second one 2 level nested with @if condition while first one is only 1 level nested.

Image

Reproduction

StackBlitz link: https://stackblitz.com/edit/stackblitz-starters-3oipce8i?file=src%2Fmain.ts
Steps to reproduce:

  1. Simply just click on any input and click outside

Expected Behavior

mat-error should displayed correctly even if it's nested inside multiple @if s.

Actual Behavior

mat-error inside multiple @if is not being displayed correctly.

Environment

  • Angular: 19.0.7
  • CDK/Material: 19.0.7
  • Browser(s): Firefox
  • Operating System (e.g. Windows, macOS, Ubuntu): Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue needs to be triaged by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions