[Resolved] How to set default value on ion-select

I would like to set a default value to Option Button on initial load.

Here’s is my code but nothing is selected when page load.

HTML file:

    <ion-item>
      <ion-label>Date</ion-label>
      <ion-select [(ngModel)]="selected_month">
        <ion-option *ngFor="let m of months" value="{{m}}" [selected]="selected_month == m">{{ m }}</ion-option>
      </ion-select>
      <ion-select [(ngModel)]="selected_year">
        <ion-option *ngFor="let y of years" value="{{y}}" [selected]="selected_year == y">{{ y }}</ion-option>
      </ion-select>
    </ion-item>

TS file:

  selected_month: any;
  selected_year: any;

  months: string[] = ['2015', '2016', '2017'];
  years: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

constructor()
{
    this.selected_month = 'Aug';
    this.selected_year = '2017';
}

Tried using both selected property and setting the value on TS file without luck.

Did I miss a step?

Found the issue. My bad, I had interchanged month and year values :smiley:

2 Likes