【问题标题】:Iterate an enum in ion-select在离子选择中迭代枚举
【发布时间】:2019-10-18 00:01:52
【问题描述】:

如何使用 ngModel 使用 ion-select 迭代枚举

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>

我需要在 ion-select-option 中列出项目并将选择的项目提交到“this.p”

【问题讨论】:

    标签: angular typescript ionic-framework enums ionic4


    【解决方案1】:

    在枚举上迭代值。您可以编写一个函数来从枚举中提取值。

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2011-10-06
      相关资源
      最近更新 更多