0

I am pretty new in js and angularjs and ran into some basic problem i can't solve. I add the $scope.consignations to the view with ng-repeat. Its a big object with several layers. In the sample code, i assign the $scope.consignations to the temp and after that, i "navigate" inside the temp object and at some point i push data to the temp. It changes the view, as expected.Now i want to clear the $scope[elementName] obj, but it clears the pushed data as well from the view. I've tried to delete the temp reference (i assume its only a reference of the $scope.consignations obj.) and i cant access it anymore, but when i clear the $scope[elementName] it clears the view anyway.

$scope.addElements = function(elementName){ 
    temp=$scope.consignations;

    for (var key in someArray) {
      //here i "navigate" recursive inside temp 
    }
    temp.push($scope[elementName]);
    delete temp;
    for (var key in $scope[elementName]) {
         $scope[elementName][key]="";
    }
};
6
  • what do you want to accomplish? Commented Nov 10, 2015 at 4:28
  • 1
    What do you think delete temp is doing? The delete operator is only used to delete properties, not objects. Commented Nov 10, 2015 at 4:29
  • I want to update the $scope.consignations with the temp, but after i've done it, i want to empty the $scope[elementName] without any other changes in the $scope.consignations or the view. Commented Nov 10, 2015 at 4:41
  • 1
    temp=$scope.consignations; by doing so your temp & $scope.consignations references same object. So change in any object will be reflected in other and also in view. So you should copy it like $scope.temp=angular.copy($scope.consignations) and then use $scope.temp for view binding and $scope.consignations for other perpose. Commented Nov 10, 2015 at 4:59
  • But i need the reference for changing the view in the first place. After that, i want to cut the reference. Commented Nov 10, 2015 at 5:53

3 Answers 3

1

temp=$scope.consignations by doing so your temp & $scope.consignations references same object. So change in any object will be reflected in other object as well as in view.

So you should copy it like $scope.temp=angular.copy($scope.consignations) and then use $scope.temp for view binding and $scope.consignations for other purpose.

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

Comments

0

If you want to copy an existing element from $scope to a new $scope variable and delete the old one then you can use the following approch.

$scope.consignations.push(angular.copy($scope[elementName]));
delete $scope[elementName];

Assuming $scope.consignations is you new array where you want to store the data and $scope[elementName] is your old one which you want to delete.

Comments

0

In case you are using definedProperty, angular will not copy these.
This snippet will help you out then.

Comments

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.