3

I'm learning Angular and I'm doing some example to understand angular.copy. What I want to do is create an Object with a Service, and then create a new Object which contains some element of the Service's Object.

Full code example: HERE

This is the "Data" Object of the Service:

return {
    name: "hello",
    toys: ["asd", "lol"],
    food: ["apple"],
    phones: ["samsung", "lg", "iphone"]
};

In the Controller I copy the Object, create a new one, and copy into it just some element of the old Object:

$scope.oldData = angular.copy(Data);
$scope.newData = {};

$scope.newData.name = oldData.name;

$scope.newData.toys = oldData.toys;

$scope.newData.phones = oldData.phones;

What I expect is that the user display just three elements: the name, the toys array and the phones array:

    <h1>{{newData.name}}</h1>

<h1>Toys:</h1>
<ul>
  <li ng-repeat="toy in newData.toys">{{ toy }}</li>
</ul>


<h1>Phones:</h1>
<ul>
  <li ng-repeat="phone in newData.phones">{{ phone }}</li>
</ul>

Why does it not work? What am I doing wrong?

1
  • Looking at the documentation, you need to specify the source and destination like this angular.copy(source, [destination]); So I would assume something like this angular.copy(Data, $scope.oldData); Commented Mar 14, 2016 at 9:16

5 Answers 5

2

oldData is scope variable, not normal javascript variable.

$scope.oldData

instead of

oldData

Code

$scope.oldData = angular.copy(Data);
$scope.newData = {};

$scope.newData.name = $scope.oldData.name;

$scope.newData.toys = $scope.oldData.toys;

$scope.newData.phones = $scope.oldData.phones;

Or either way you could make it var oldData

Demo

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

Comments

2

You have not added $scope before using oldData. Check updated plunkr http://plnkr.co/edit/znoKheLX3aMVHpNo7lh4?p=preview

 $scope.oldData = angular.copy(Data);
 $scope.newData = {};

 $scope.newData.name = $scope.oldData.name;

 $scope.newData.toys = $scope.oldData.toys;

 $scope.newData.phones = $scope.oldData.phones;

Comments

2

you re using oldData but you ve defined $scope.oldData

     var oldData = angular.copy(Data);
     $scope.newData = {};

     $scope.newData.Name = oldData.name;

     $scope.newData.toys = oldData.toys;

     $scope.newData.phones = oldData.phones;

here the fixed plunker

Comments

1

you have to write the scope object $scope.newData.name = $scope.oldData.name; ...

Comments

0

When I run your code the error i got was oldData is not defined. That is because you are using $scope.newData.Name = oldData.name; but javascript engine cannot find a variable oldData in this scope.

You have oldData attached to the $Scope. So you have to use $scope.newData.Name = $scope.oldData.name;

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.