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);

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 look at this the same problem related to console.log are appearing to be printed late stackoverflow.com/a/11284702/2435473

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.