0
votes

How to iterate an enum with ion-select using ngModel

export enum Escolaridade {
    ANALFABETO = "Analfabeto",
    DE_1_A_3ANOS = "de 1 a 3 anos",
    DE_4_A_7ANOS = "de 4 a 3 anos",
    MAIS_DE_8ANOS = "mais de 8 anos"
}

class Pessoa {
    ...
    public escolaridade:typeof Escolaridade =  Escolaridade;
}

export class CadastroPage {
    ...
    this.p = new Pessoa();
}

home.html
<ion-select interface="popover" [(ngModel)]="p.escolaridade">
<ion-select-option *ngFor="let escolaridade of p.escolaridade">       
    {{escolaridade}}
</ion-select-option>
</ion-select>

I need list items in ion-select-option and submit item selected to "this.p"

1

1 Answers

0
votes

To iterate values on an enum. You can write a function to extract values from the enum.

extractValuesFromEnum(enum)
{
  const retVal = [];
  const keys = Object.keys(enum);
  for(let i=0; i < keys.length; i++)
  {
    if(i %2 !== 0 )  // odd elements in the array will be the values.
      {
        retVal.push(keys[i])
      }
  }
  return retVal
}