Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

356
Vistas
How to hide a HTML element using an if-statement?

My application allows the user to upload files to a database, I also have a mat-select component where the user is able to select which excel worksheet they would like to upload.

I would like to hide this mat-select component IF the user selects a CSV file, however my [hidden]="hideDropdown" doesn't seem to work.

hideDropdown: boolean;

if (this.file.type == "text/csv") {
  this.hideDropdown = true;
} else {
  this.hideDropdown = false;
}
<mat-form-field *ngIf="sourceFile" id="worksheetStyle" appearance="fill">
  <mat-label>Excel Worksheet</mat-label>
  <mat-select [hidden]="hideDropdown" [(ngModel)]="selectedSheet" (selectionChange)="selectWorksheet()">
    <mat-option *ngFor="let worksheet of worksheetNames" [value]="worksheet">
      {{worksheet}}
    </mat-option>
  </mat-select>
</mat-form-field>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I believe that the reason [hidden] attribute isnt working is because it doesnt exist on mat-select only on html-tags as it is a core html attribute. What you could do instead is use *ngIf as follows:

  <mat-select *ngIf="hideDropDown" [(ngModel)]="selectedSheet" 
  (selectionChange)="selectWorksheet()">
   <mat-option *ngFor="let worksheet of worksheetNames" [value]="worksheet">
     {{worksheet}}
   </mat-option>
 </mat-select>

However it should be noted that the *ngIf will remove the select from the DOM entirely whilst [hidden] is just applying a display: none to the element thus having it still be rendered in the DOM. So if you've created a reactive form that expects to have a select input it will cause error if you remove it with ngIf. However it seems that you are just using the select by itself and with a ngModel so that shouldnt be an issue for you.

Edit 1:

Working stackblitz showing a possible solution: https://stackblitz.com/edit/angular-5ykmue?file=src/app/select-overview-example.html

Edit 2:

I stand corrected with not being able to use the [hidden] attribute, if you wrap the form-field in a div you can then use hidden to show / hide as follows:

<div [hidden]="hideDropdown">
<mat-form-field *ngIf="sourceFile" id="worksheetStyle" appearance="fill">
  <mat-label>Excel Worksheet</mat-label>
  <mat-select [(ngModel)]="selectedSheet" (selectionChange)="selectWorksheet()">
    <mat-option *ngFor="let worksheet of worksheetNames" [value]="worksheet">
      {{worksheet}}
    </mat-option>
  </mat-select>
</mat-form-field>
</div>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda