2

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.

13
  • Actually you're setting the ngModel to the same variable that is the source (array). You have to create another variable and put it in ngModel. Also is keys a pipe? If so, it would be better if you paste it here. Commented Feb 21, 2017 at 13:01
  • yes keys is a pipe .. with which i am accessing key's and value. Do you think that this issue is because from december the first array key is 10 here first is 1. whereas till december first of key is 0 Commented Feb 21, 2017 at 13:09
  • I changed to allMonths = {'1':'January','2':'February','3':'March','4':'April','5':'May','6':'June','7':'July','8':'August','9':'September','10':'October','11':'November','12':'December'};.... now i am getting the desired response but i want that keys not like this can any one suggest how i can achieve this ? Commented Feb 21, 2017 at 13:21
  • As I said, you should paste your pipe here, so it'll be easier to help you. Commented Feb 21, 2017 at 13:22
  • import {Pipe, PipeTransform} from '@angular/core'; @Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value) : any { let keys = []; for (let key in value) { keys.push({key: key, value: value[key]}); } return keys; } } Commented Feb 21, 2017 at 13:26

1 Answer 1

2
    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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.