问题
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