AngularJS: angular.copy with example

From AngularJS documentation

angular.copy : angular.copy(source, [destination]);

Creates a deep copy of source, which should be an object or an array.
  1. If no destination is supplied, a copy of the object or array is created.
  2. If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.
  3. If source is not an object or array (inc. null and undefined), source is returned.
  4. If source is identical to 'destination' an exception will be thrown.

1. If no destination is supplied, a copy of the object or array is created.
<body ng-app="demoApp">
<div ng-controller="DemoController">
  1. If no destination is supplied, a copy of the object or array is created.
 New Destination:{{firstScenario}}
</div>
</body>
var app = angular.module('demoApp', [])
   .controller('DemoController', function($scope) {
    // 1. If no destination is supplied, a copy of the object or array is created.
    $scope.firstSource = $scope.source = {
   one:"one",
   two:[{
    three:"three"
   },{
    four:"four"
   }],
   five:["six","seven"]
    };
    $scope.firstScenario = angular.copy($scope.firstSource);
   });









2.  If a destination is provided, all of its elements (for arrays) or properties (for objects) are deleted and then all elements/properties from the source are copied to it.

<body ng-app="demoApp">
<div ng-controller="DemoController">
  2. If a destination is provided, all of its elements (for arrays) or 
 properties (for objects) are deleted and then all elements/properties from the source are copied to it.
$scope.secondScenario = angular.copy($scope.secondSource, $scope.secondDestination);
  {{secondScenario}}
</div>
</body>

var app = angular.module('demoApp', [])
   .controller('DemoController', function($scope) {
$scope.secondSource = {
   one:"one",
   two:[{
    three:"three"
   },{
    four:"four"
   }],
   five:["six","seven"]
    };
    
    $scope.secondDestination = {
   one:"one",
   five:["six","seven"],
   two:[{three:"three"},{four:"four"}]
    };
    $scope.secondScenario = angular.copy($scope.secondSource, $scope.secondDestination);
});






3. If source is not an object or array (inc. null and undefined), source is returned.

 $scope.thirdScenario = angular.copy($scope.thirdSource, $scope.thirdDestination)

 {{thirdScenario}}
$scope.thirdSource;
    
    $scope.thirdDestination = {
   one:"one",
   five:["six","seven"],
   two:[{three:"three"},{four:"four"}]
    };
    $scope.thirdScenario = angular.copy($scope.thirdSource, $scope.thirdDestination);









4. If source is identical to 'destination' an exception will be thrown.
 $scope.fourthScenario = angular.copy($scope.fourthSource, $scope.fourthSource)
 {{fourthScenario}}
$scope.fourthSource = {
   four:"four"
    };
    $scope.fourthScenario = angular.copy($scope.fourthSource, $scope.fourthSource);
















Plunker: https://plnkr.co/edit/lehhy7cC5pNl8YFDpSs9?p=preview

Good Day :)

Post a Comment

0 Comments