0

Not fully understanding my scope issue here in angular and looking for clarity and how to fix this issue. I have an array of strings that I get from a service. What I'm trying to do is store those strings in one scope variable.

When my ui-select="loadDealStuff($item)" is run I have a on-select method that passes the selected object in. If the key of that is a certain value then i'm removing some elements from my array. Other wise if its not then I need the original values from the array. Here is the code:

scope.productTypes = myService.constantsStrings.myProductTypes;                
scope.otherTypes = myService.constantsStrings.myProductTypes;

var x = scope.productTypes;//array I want to manipulate
var y = scope.otherTypes;//should be original values

scope.loadDealStuff= function(item){
  if(item.key == 3){                                                                        
    x.splice(0,1);
    x.splice(2,1);
    console.log(x);
  } else {                                                                
    console.log(y);
  }
};

When logged y is the same as x.

Gonzalo.-
  • 12,512
  • 5
  • 50
  • 82
Troy Bryant
  • 994
  • 8
  • 29
  • 60
  • 2
    Both `scope.productTypes` and `scope.otherTypes` have a reference to the same array (the result of your service), by extension then both 'x' and 'y' are referencing the same array as well, since you're assigning the reference of `scope.productTypes` and `scope.otherTypes` respectively. You'd have to create a copy of the array instead of assigning it. Check [this question](http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript) for more information. – Osman Cea Feb 07 '17 at 17:38
  • angular.copy your stuff. – Mikko Viitala Feb 07 '17 at 18:10

0 Answers0