Select default option not working with Ionic2 Framework

hello,
i’m looking to do default selected value to my option. when i press on the button to choose it be by defaulted selected to the first item of the options.
this is my code

<ion-list>
  <ion-item>
    <ion-label >choose course</ion-label>
    <ion-select  [(ngModel)]="courseSelect" (ionChange)="onCourseChange($event)"  [ngStyle]="{'direction':'rtl'}" okText="בחר" cancelText="בטל">
      <ion-option *ngFor="let course of coursesNames"  [value]="course" selected>{{course}}</ion-option>
    </ion-select>
  </ion-item> 
  <ion-item *ngIf="courseSelect" class="mar">
    <ion-label >choose lecture</ion-label>
    <ion-select [(ngModel)]="lectureSelect" (ionChange)="onLectureChange($event)" okText="בחר" cancelText="בטל">
      <ion-option [ngStyle]="{'direction':'rtl'}" *ngFor="let lecture of LecturesNames" [value]="lecture">{{lecture}}</ion-option>
    </ion-select>
  </ion-item>

i wrote "selected but it’s not doing nothing.

You have “selected” on every single option, which doesn’t make much sense. Why not just seed courseSelect with whatever you want the default to be?

because i’m don’t know what will be the default. it change by request from server.

Perhaps somebody else can make more sense out of what you’re saying, but there isn’t any way that you can set a default if you don’t know what you want the default to be.

this.courseSelect=this.coursesNames[0];

i succeed to be like that but i’m not sure if that’s good way.

That is exactly what I meant by “seeding courseSelect”. That’s what I would do.

1 Like