How to Set Placeholder on Select Tag in Angular 8?
Asked Answered
T

9

14

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 .

Transmundane answered 23/12, 2019 at 5:58 Comment(0)
H
14

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>
Heterosis answered 23/12, 2019 at 6:2 Comment(2)
I had to do it for dynamic dropdowns scenario inside a for loop and setting to null was not feasible for me. I tried [ngValue]="undefined" [disabled]="true" and it worked in my case because the empty objects had undefined by default.Mercurate
Instead of [disabled]="true" you can just use disabledDebut
P
18

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>
Pristine answered 30/10, 2020 at 13:44 Comment(0)
H
14

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>
Heterosis answered 23/12, 2019 at 6:2 Comment(2)
I had to do it for dynamic dropdowns scenario inside a for loop and setting to null was not feasible for me. I tried [ngValue]="undefined" [disabled]="true" and it worked in my case because the empty objects had undefined by default.Mercurate
Instead of [disabled]="true" you can just use disabledDebut
G
4

This works for me when model doesn't have initial value

<select [(ngModel)]="myModel">
        <option [ngValue]="undefined" [disabled]="true">Choose Type</option>
        <option *ngFor="let type of typeList; let i = index;" [value]="type.value">{{type.label}}</option>
</select>
Goldengoldenberg answered 20/7, 2022 at 23:9 Comment(1)
this solution worked after l searched for longKowatch
M
3

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

Mezuzah answered 23/12, 2019 at 6:6 Comment(0)
P
2

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

Phenomenon answered 23/12, 2019 at 6:10 Comment(0)
T
2

This works fine for me

> <select [(ngModel)]="name">      
> <option value="" disabled selected hidden>Choose your number</option>   
> <option value="red">One</option>    
> <option value="green">Two</option>     
> <option value="black">Three</option> 
></select>
Tillo answered 9/2, 2023 at 3:55 Comment(0)
Q
0

<select id="district" class="form-control">
  <!-- <option [disabled]="true">Select District/City</option> -->
  <option hidden value="" disabled selected>Select District/City</option>
  <option *ngFor="let postoffice of filterPostOffice" [value]="postoffice.District">
    {{ postoffice.District }}
  </option>
</select>

Placeholder in Select

Quathlamba answered 7/11, 2020 at 1:50 Comment(1)
value attribute should be a blank string if you want to make a particular option to default option. new FormControl(['']) component.html value attribute should be a blank string eg: value=''"Mildredmildrid
T
0

This works fine for me:

<option value="" disabled selected>Text to display</option>
Thrush answered 2/12, 2020 at 19:55 Comment(0)
K
0

In Angular, the option you want to act as a placeholder set the value="undefined" and place it on the very top as below:

<select
  id="fmonthsCommitment"
  name="support"
  class="inp-si"
  (click)="commitmentNotification()"
  [(ngModel)]="commitmentInMonthsInput"
>
  <option value="undefined" disabled selected>
    Choose Months Commitment...
  </option>
  <option value="3">3</option>
  <option value="6">6</option>
  <option value="12">12</option>
  <option value="24">24</option>
</select>
Kellum answered 25/11, 2021 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.