Reputation: 729
okay, this seems to be a weird one
i have a catalogue filled with items using an ng-repeat
from an array called items
, when the user selects an item, a dialogue appears asking the user for some extra information (just now, only a new username), when they hit submit, an addToCart(item)
function is called and passes the selected item.
as part of the function it changes the Question[]
variable in the item
object to look like this Question = "question:answer"
here's where it get's weird, the very first thing this function does is print the passed item to the log. when it does, it shows that the function has already run. the next line prints out the items
array, which shows that the changes to item that was passed have been applied to the original item.
to the best of my knowledge, a passed variable is treated as a copy of the original value no? 0.o
can anybody see what i've done wrong?... or have any thoughts on the solution for these issues?
here's the code:
the HTML:
<div class="panel-footer" ng-show="cat.itemHasQuestions(item)">
<a href="" class="btn btn-primary" data-toggle="modal" data-target="#cartDataU" ng-click="cat.selected = item">Add to order</a>
<input class="pull-right" type="number" value="1" min="0" ng-model="item.qty"/>
<label class="pull-right">QTY </label>
</div>
<div id="cartDataU" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Please provide more details.</h4>
</div>
<div class="modal-body">
Content:
<form>
<input type="text" ng-repeat="info in cat.selected.Question track by $index" placeholder="{{info[0] + '...'}}" class="form-control" ng-model="cat.selected.answer[$index]">
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default btn-block" data-dismiss="modal" ng-click="cat.addToCart(cat.selected, 'user')">submit</button>
</div>
</div>
</div>
</div>
and the addToCart function:
this.addToCart = function(item, cat){
console.log("====================");
console.log(item);
console.log(catCon.items);
console.log("====================");
if(item.qty == undefined || item.qty <=0){
//console.log("qty undefined")
item.qty = 1
//console.log("updated QTY");
//console.log(item.qty);
}
/* console.log(item); */
if(catCon.itemHasQuestions(item, false, "Code: addtocart") == true){
/* console.log("================");
console.log(item.Question);
console.log("================"); */
qa = "";
for(i = 0; i<item.Question.length; i++){
if(i < item.Question.length-1){
qa += item.Question[i][0] + ":" + item.answer[i] + ",";
}else{
qa += item.Question[i][0] + ":" + item.answer[i];
}
}
item.Question = qa;
}else{
// console.log("Question is a string in an array, making string in string xD")
item.Question = "";
}
if(catCon.itemIsInCart(item.Name)){
catCon.cart[catCon.findItemInCart(item.Name)].qty++;
catCon.cart[catCon.findItemInCart(item.Name)].Question += "," + qa;
}else{
catCon.cart[catCon.cart.length] = item;
}
}
if you need to see anything else, just ask and i'll provide. thanks
Upvotes: 0
Views: 31
Reputation: 61
Nope, the object passed is a reference to the object on the list. Not a copy of it.
You can use angular.copy() to deep copy the object for the cart and modify that.
Upvotes: 2