Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular4 placeholder for a select

Tags:

select

angular

I'm trying to add a placeholder to a select on Angular 4 but no way to make it works,

Here is my code :

<select name="edit" [(ngModel)]="edit">
        <option [ngValue]="null" disabled [selected]="true"> Please select one option </option>
        <option *ngFor="let select of list" [ngValue]="edit"> {{ select }}</option>
</select>


export class AppComponent  {
  list: any[] = ['1', '2', '3'];
  edit: any;
}
like image 252
error Avatar asked Sep 11 '17 17:09

error


4 Answers

I have created plunker. Hope this will helps.

 <select name="edit" [(ngModel)]="edit">
    <option [ngValue]="undefined" disabled  selected> Please select one option </option>
    <option *ngFor="let select of list" [ngValue]="edit"> {{ select }}</option>
  </select>
export class AppComponent  {
  list: any[] = ['1', '2', '3'];
  edit: any;
}
like image 178
alexKhymenko Avatar answered Oct 12 '22 04:10

alexKhymenko


can you try this, in template

<select name="edit" [(ngModel)]="edit">
    <option value=""> Please select one option </option>
    <option *ngFor="let select of list" value="{{select}}"> {{ select }}</option>
</select>

your component.ts

edit: string = '';
like image 37
Robert Avatar answered Oct 12 '22 04:10

Robert


If you wish to have the placeholder selected by default:

<select name="edit" [(ngModel)]="edit">
    <option value="0" disabled selected>Select your option</option>
    <option *ngFor="let select of list" [value]="select"> {{ select }}</option>
</select>

DEMO

like image 39
Vega Avatar answered Oct 12 '22 06:10

Vega


I know its an old question and an answer has been selected, however I felt there was one small improvement that I could add. Include 'hidden' on your placeholder and that should eliminate it being visible in the dropdown.

<select name="edit" [(ngModel)]="edit">
   <option value="0" disabled selected hidden>Select your option</option>
   <option *ngFor="let select of list" [value]="select"> {{ select }}</option>
</select>
like image 40
jgritten Avatar answered Oct 12 '22 06:10

jgritten