Let me give you the code first and then I'll describe the problem.
import { Component } from '@angular/core';
import { IonicPage, NavParams, ViewController } from 'ionic-angular';
import { StuffsProvider } from '../../providers/stuffs/stuffs';
@IonicPage()
@Component({
selector: 'page-item-menu',
templateUrl: 'item-menu.html',
})
export class ItemMenuPage {
item: any;
amount: number[] = [];
rememberAmount: number[] = [];
constructor(private navParams: NavParams,
private viewCtrl: ViewController,
private stuffsProvider: StuffsProvider) {
}
ngOnInit(): void {
this.item = this.navParams.get('item');
let index = -1;
for(let stuff of this.stuffsProvider.stuffs) {
if(stuff.name === this.item.name){
index = this.stuffsProvider.stuffs.indexOf(stuff);
}
}
if(index > -1){
this.amount = this.stuffsProvider.stuffs[index].amount;
} else {
this.amount = [1];
if(this.item.size.length > 1){
this.amount = [];
for(let stuff of this.item.size){
this.amount.push(0);
}
}
}
this.rememberAmount = this.amount;
alert(this.rememberAmount[0]);
}
addAmount(index): void {
this.amount[index] = this.amount[index] + 1;
}
subtractAmount(index): void {
if(this.amount[index] > 0){
this.amount[index] = this.amount[index] - 1;
}
alert(this.rememberAmount);
}
confirm(): void {
this.stuffsProvider.pushItem(this.item, this.amount);
this.viewCtrl.dismiss();
}
close(): void {
this.amount = this.rememberAmount;
this.viewCtrl.dismiss();
}
}
My problem here is that somehow rememberAmount is always the same as amount.
I just wanted to write a template with which the user can increase the amount of an item or decrease it (addAmmount and subtractAmmount - buttons). The user should also be able to close this template using the close() function and the corresponding abort button to not change anything. But somehow the amount and the rememberAmount always get changed in the same way, no matter if I press the abort button or the confirm button (I mean for the confirm button this behaviour is desired, but ...).
Does the = sign make the arrays the same array instead of just transmitting each others value, so that both names (amount and rememberAmount) point to the same object?