ionic2 + Angular2 selection of list

2.1k views Asked by At

http://ionicframework.com/docs/v2/components/#select

I'm trying to show select option and capture the selection. I have tried from method, onChanges and even on putting inline on click, but nothing works.

<ion-list>
    <ion-item>
        <ion-label>List </ion-label>
        <ion-select [(ngModel)]="selected" multiple="true" cancelText="Cancel" okText="Okay!">
            <ion-option *ngFor="#row of list;" value="{{row.id}}" (click)="checkSelected(row.id)" checked="{{ gq.isSelected(row.id) }}">{{ row.english_name }}</ion-option>
        </ion-select>
    </ion-item>
</ion-list>

Setting page class

export class SettingPage implements OnChanges{

    selected;
    list = [];

    constructor(private nav:NavController, public gq: gq)
    {
        this.gq
            .list()
            .subscribe(data => this.list.push(data));
    }
    stpSelect() {
        console.log('STP selected');
        console.log(this.selected);

    }
    ngOnChanges(changes) {
        console.log(changes);
    }

    checkSelected (data)
    {
        console.log('test');
        console.log(data);
    }
}

I just want to catch selected model values from a function, when things are changed on it.

1

There are 1 answers

0
Thierry Templier On BEST ANSWER

Since the ion-select component leverages ngModel, you could try to listen to the ngModelChange event:

<ion-select [(ngModel)]="selected" multiple="true" cancelText="Cancel" okText="Okay!" (ngModelChange)="doSomething()">
  (...)
</ion-select>