Toogle Button is capturing two objects with one click


#1

Guys, how’s it going? I need a help with a toogle button.
I build a list of products, where each product has a toogle button, and the idea is, when I check the toogle button this marked item goes to an array, that array will be of the products that the user will buy, and will be passed as parameter to the shopping cart page.

Currently my list of items has only two items. And when I select just ONE of these items, it marks the TWO items, I can not find what I did wrong

  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              public produtosProvider: ProdutosProvider) { 

              this.produtosProvider.getAll()
                .subscribe( produtos=> {
                  //I capture all the products of the database
                  this.products = produtos;
                  this.loadedProductList = produtos;
                  //here I just create an array with the names of the products
                  this.products.map( prod => {
                    this.productsSale.push(prod.nome);
                  })
                })

              this.productsSale = [];

  }
  productsCart(event) {
    //here I filter the array of product names
    if (event.checked == true) {
      let test = this.productsSale.filter(teste => {
        //here I expect to return only the items that are marked on the toogle button
        return event.checked;
      })
      console.log(test);
    }
  }
 <ion-toggle [(ngModel)]="item.value" class="toogle-button"
                    (ionChange)="productsCart($event)">
 </ion-toggle>


#2

Think about this loop for a minute.

What changes on each iteration?

What effect could that have on what each iteration returns?


#3

To be honest with you, I got this tip in a tutorial and I tried to implement it here. And it’s the first time that I move with toogle.
Forgive me for the bad English.


#4

Another thing, this toogle button, is not a property of the products, I put it there just to be able to make a comparison so that I pass the selected items with it by parameter to another screen.


#5

Já consegui resolver o problema