Array with custom indexes in Ionic2

僤鯓⒐⒋嵵緔 提交于 2020-03-02 09:41:25

问题


I am creating an array in a variable in Ionic2 as:

allMonths = {'01':'January','02':'February','03':'March','04':'April','05':'May','06':'June','07':'July','08':'August','09':'September','10':'October','11':'November','12':'December'};

I want to get all months (with keys specified) i am displaying them in html as :

<ion-item>
    <ion-select [(ngModel)]="allMonths">
        <ion-option value="{{months.key}}" *ngFor = "let months of allMonths | keys">{{months.value}}</ion-option>
    </ion-select>
</ion-item>

** Although i am getting response but the issue is i am getting as:

1st: October 2nd: November 3rd: December 4th: January . . . 12th: September

***** But i want them to be in series from jan to dec in popup.

Can any body suggest please where i am wrong.

Thanks in advance.


回答1:


    After a long google I solved it as:

        allMonths:Array<Object> = [
       {id: '01', text: 'January'},
        {id: '02', text: 'February'},
        {id: '03', text: 'March'},
        {id: '04', text: 'April'},
        {id: '05', text: 'May'},
        {id: '06', text: 'June'},
        {id: '07', text: 'July'},
        {id: '08', text: 'August'},
        {id: '09', text: 'September'},
        {id: '10', text: 'October'},
        {id: '11', text: 'November'},
        {id: '12', text: 'December'},
    ];

    In Html:

<ion-item>
        <ion-select [(ngModel)]="allMonths">
            <ion-option value="{{months.id}}" *ngFor = "let months of allMonths ">{{months.id}}</ion-option>
        </ion-select>
    </ion-item>

Hope it helps someone.



来源:https://stackoverflow.com/questions/42367946/array-with-custom-indexes-in-ionic2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!