Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set Placeholder on Select Tag in Angular 8?

I am creating a dropdown to filter data so I have created a dropdown

<form  #filter="ngForm" (ngSubmit)="filterta(filter)" style="display: flex;">
       <select  ngModel #month="ngModel" name="month" required  >
                <option [ngValue]="null" [disabled]="true" >All</option>
                <option value="1">January</option>
       </select>
</form>

when i remove ngModel #month="ngModel" these properties form select tag it shows placeholder option All and when i add these properties it shows Blank in Placeholder .

like image 278
Nitin tiwari Avatar asked Dec 23 '19 05:12

Nitin tiwari


4 Answers

This worked for me in Angular-10.

<select class="form-control" name="role" id="role" formControlName="role">
     <option value="" disabled selected>Select the role</option>
     <option *ngFor="let role of roleList" value="{{ role }}">{{role}}</option>
</select>
like image 165
Malith Avatar answered Sep 24 '22 23:09

Malith


Set initial value of month = null in the component.ts and add [(ngModel)]="month" in the select tag of component.html.

component.ts

month = null;

component.html

<form  #filter="ngForm" (ngSubmit)="filterta(filter)" style="display: flex;">
       <select name="month" [(ngModel)]="month" #month required>
                <option [ngValue]="null" [disabled]="true" >All</option>
                <option value="1">January</option>
       </select>
</form>
like image 29
Surjeet Bhadauriya Avatar answered Sep 22 '22 23:09

Surjeet Bhadauriya


Try like this:

  • Modify ngModel #month="ngModel" to [(ngModel)]="selectedMonth" #month
  • In .ts file, add selectedMonth = null;

.ts

selectedMonth = null;

.html

<form  #filter="ngForm" (ngSubmit)="filterta(filter)" style="display: flex;">
       <select  [(ngModel)]="selectedMonth" #month name="month" required  >
                <option [ngValue]="null" [disabled]="true" >All</option>
                <option value="1">January</option>
       </select>
</form>

Working Demo

like image 35
Adrita Sharma Avatar answered Sep 24 '22 23:09

Adrita Sharma


You need to change your code a little bit

html file

[(ngModel)]="month" // This will set month value for select tag

in ts file

Set month value to deafult value whatever you want like

month = null;

Working code

https://stackblitz.com/edit/angular-plwvgg

like image 35
Passionate Coder Avatar answered Sep 25 '22 23:09

Passionate Coder