Creates a deep copy of source
, which should be an object or an array.
source
is not an object or array (inc. null
and undefined
), source
is returned.source
is identical to destination
an exception will be thrown.source
and on destination
) will be ignored.
angular.copy(source, [destination]);
Param | Type | Details |
---|---|---|
source | * |
The source that will be used to make a copy.
Can be any type, including primitives, |
destination
(optional)
|
ObjectArray |
Destination into which the source is copied. If
provided, must be of the same type as |
* | The copy or updated |
<div ng-controller="ExampleController">
<form novalidate class="simple-form">
<label>Name: <input type="text" ng-model="user.name" /></label><br />
<label>Age: <input type="number" ng-model="user.age" /></label><br />
Gender: <label><input type="radio" ng-model="user.gender" value="male" />male</label>
<label><input type="radio" ng-model="user.gender" value="female" />female</label><br />
<button ng-click="reset()">RESET</button>
<button ng-click="update(user)">SAVE</button>
</form>
<pre>form = {{user | json}}</pre>
<pre>master = {{master | json}}</pre>
</div>
// Module: copyExample
angular.
module('copyExample', []).
controller('ExampleController', ['$scope', function($scope) {
$scope.master = {};
$scope.reset = function() {
// Example with 1 argument
$scope.user = angular.copy($scope.master);
};
$scope.update = function(user) {
// Example with 2 arguments
angular.copy(user, $scope.master);
};
$scope.reset();
}]);