Pre-select first element of the array

Asked

Viewed 245 times

1

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.installments);

(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)"}

How can I pre-select the first element of the array? So that the first option is always selected.

0: {type: "1"value: "1x de R$ 38,50 (sem juros)"}
  • try like this <ion-select class="myCustomSelect" [(ngModel)]="formaVezes" [ngModelOptions]="{standalone:true}">&#xA; <ion-option *ngFor="let p of parcelamentosArray" [value]="p.type" [selected]="p[0]" > {{ p.value}} </ion-option>&#xA; </ion-select>

  • ERROR Typeerror: Cannot read Property 'type' of Undefined

  • Use the directive ngModelChange to detect changes in your select. See this answer: https://stackoverflow.com/a/41160860/2570426

  • Unsolved!

  • try this p?. type like this

  • 1

    When you load/assemble the "installments" Array in Typescript, or start the view, make the "forms" receive "installments Rray[0]" (formaVezes = installments Rray[0]). You don’t have to do it on the screen, do it on the controller.

  • I no longer have access to this project, but stopping to reflect since way would work... Thanks for the reply!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.