1

hello I have one a array $scope.name .I am spliting the array into small arrays .But after spliting the array .it become blank why ? actually I assigned the given array into temp variable and splite the temp variable .Again my $scope.name become blank why ?

here is my plunker http://plnkr.co/edit/iUscrw0xclHSnsIWMMTM

    console.log("before");
    console.log($scope.name);
    var test=$scope.name;
     console.log("after");
      console.log($scope.name);
    console.log("test");
    console.log(test);
    var arrays = [], size = 3;

while (test.length > 0)
    arrays.push(test.splice(0, size));

console.log(arrays);
console.log("name");
    console.log($scope.name);
user944513
  • 12,247
  • 49
  • 168
  • 318

1 Answers1

1

You are directly assigning object to other object, so that will cause change in any of the object will update other object value. Use angular.copy instead of assigning object directly, that will create a new clone copy of that object will returned.

 var test=angular.copy($scope.name);

Forked Plunkr

Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
  • Why do the `console.logs` appear empty, even though they're executed before the while loop? Just wondering since this seems like really weird behaviour. – Fissio Jun 09 '15 at 12:17
  • @Fissio look at this the same problem related to `console.log` are appearing to be printed late http://stackoverflow.com/a/11284702/2435473 – Pankaj Parkar Jun 09 '15 at 12:24