Select first element in array

#1

Select first element in array

My Select

 <ion-item>
   <ion-label floating color="primary" interface="popover">Opções de Parcelamento</ion-label>
   <ion-icon name="pricetags"></ion-icon>
   <ion-select class="myCustomSelect" [(ngModel)]="formaVezes" [ngModelOptions]="{standalone:true}">
     <ion-option *ngFor="let p of parcelamentosArray" [value]="p.type" [selected]="p.type[0]" > {{ p.value}}  </ion-option>
  </ion-select>
</ion-item>

My Array

console.log(this.parcelamentosArray);

(8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {type: "1"value: "1x de R$ 38,50 (sem juros)"}
1: {type: "2"value: "2x de R$ 20,12 - Total: R$ 40,24 (2.99% a.m)"}
2: {type: "3", value: "3x de R$ 13,61 - Total: R$ 40,82 (2.99% a.m)"}
3: {type: "4", value: "4x de R$ 10,36 - Total: R$ 41,42 (2.99% a.m)"}
4: {type: "5", value: "5x de R$ 8,40 - Total: R$ 42,02 (2.99% a.m)"}
5: {type: "6", value: "6x de R$ 7,11 - Total: R$ 42,63 (2.99% a.m)"}
6: {type: "7", value: "7x de R$ 6,18 - Total: R$ 43,24 (2.99% a.m)"}
7: {type: "8", value: "8x de R$ 5,48 - Total: R$ 43,86 (2.99% a.m)"}

what I can do for pre select this element first?

0: {type: "1"value: "1x de R$ 38,50 (sem juros)"}
#2

there is only one small change.

<ion-option *ngFor="let p of parcelamentosArray" [value]="p.type" [selected]="p[0]" > {{ p.value}}  </ion-option>

:slight_smile:

#4

Of course your code is correctly, but still don’t working on my app

#5

ok let do it in different way,
in html file

<ion-option *ngFor="let p of parcelamentosArray" [value]="p.type" [selected]="p[0].type == temp" > {{ p.value}}  </ion-option>

and in ts file

this.temp = parcelamentosArray[0].type

this is because selected take only boolean value either true or false.

#6

Again give a error.
If use [selected]="parcelamentosArray[0].type == temp" , works… But not in correct way.

#7

what exact error you got?

#8

See in the link! error .