Reputation: 53
Assuming my driver has an id, how do I pass its value to the next page?
html file
<ion-item *ngFor="let driver of items; let i = index ">
<ion-label>{{driver.name}}</ion-label>
<ion-checkbox [(ngModel)]="driver[i]" item-right></ion-checkbox>
</ion-item>
Example of the value fo the checkbox does not appear at all
ts file
initializeItems() {
this.drivers = [];
this.driver.getDrivers((data)=>{
console.log(data);
for(let i in data.drivers){
this.drivers.push({name: data.drivers[i].name, id: data.drivers[i].id})
}
this.items = this.drivers;
})
}
Upvotes: 1
Views: 327
Reputation: 2736
Try with the below example. you can add a one more property to driver object to track the selection. check the working version here
home.html
<ion-header>
<ion-navbar>
<ion-title>Home</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<ion-list>
<ion-item *ngFor="let ing of pizzaIng; let i = index">
<ion-label>{{ing.name}}</ion-label>
<ion-checkbox [(ngModel)]="ing.checked" (ionChange)="updateIng()"></ion-checkbox>
</ion-item>
</ion-list>
</ion-content>
home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
pizzaIng : any;
selectedIng : any=[];
constructor(public navCtrl: NavController) {
this.pizzaIng=[
{name : "Pepperoni", checked : true},
{name : "Sasuage", checked : true},
{name : "Mushrooms", checked : true}
]
}
updateIng(){
this.selectedIng=[];
for(let ingr of this.pizzaIng){
if(ingr.checked === true){
this.selectedIng.push(ingr);
}
}
console.log(this.selectedIng);
}
}
Upvotes: 1