Skip to content

Commit 14519f8

Browse files
committed
docs(copy): mention ignoring non-enumerable properties
This also improves the example a bit: - better code formatting - initialization of `form` to an empty object - avoid using `email`, which doesn't get coppied when invalid (and might confuse users) Fixes angular#14853
1 parent 3aedb1a commit 14519f8

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

src/Angular.js

+41-34
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,13 @@ function arrayRemove(array, value) {
751751
* * If a destination is provided, all of its elements (for arrays) or properties (for objects)
752752
* are deleted and then all elements/properties from the source are copied to it.
753753
* * If `source` is not an object or array (inc. `null` and `undefined`), `source` is returned.
754-
* * If `source` is identical to 'destination' an exception will be thrown.
754+
* * If `source` is identical to `destination` an exception will be thrown.
755+
*
756+
* <br />
757+
* <div class="alert alert-warning">
758+
* Only enumerable properties are taken into account. Non-enumerable properties (both on `source`
759+
* and on `destination`) will be ignored.
760+
* </div>
755761
*
756762
* @param {*} source The source that will be used to make a copy.
757763
* Can be any type, including primitives, `null`, and `undefined`.
@@ -760,41 +766,42 @@ function arrayRemove(array, value) {
760766
* @returns {*} The copy or updated `destination`, if `destination` was specified.
761767
*
762768
* @example
763-
<example module="copyExample">
764-
<file name="index.html">
765-
<div ng-controller="ExampleController">
766-
<form novalidate class="simple-form">
767-
Name: <input type="text" ng-model="user.name" /><br />
768-
E-mail: <input type="email" ng-model="user.email" /><br />
769-
Gender: <input type="radio" ng-model="user.gender" value="male" />male
770-
<input type="radio" ng-model="user.gender" value="female" />female<br />
771-
<button ng-click="reset()">RESET</button>
772-
<button ng-click="update(user)">SAVE</button>
773-
</form>
774-
<pre>form = {{user | json}}</pre>
775-
<pre>master = {{master | json}}</pre>
776-
</div>
777-
778-
<script>
779-
angular.module('copyExample', [])
780-
.controller('ExampleController', ['$scope', function($scope) {
781-
$scope.master= {};
782-
783-
$scope.update = function(user) {
784-
// Example with 1 argument
785-
$scope.master= angular.copy(user);
786-
};
769+
<example module="copyExample">
770+
<file name="index.html">
771+
<div ng-controller="ExampleController">
772+
<form novalidate class="simple-form">
773+
<label>Name: <input type="text" ng-model="user.name" /></label><br />
774+
<label>Age: <input type="number" ng-model="user.age" /></label><br />
775+
Gender: <label><input type="radio" ng-model="user.gender" value="male" />male</label>
776+
<label><input type="radio" ng-model="user.gender" value="female" />female</label><br />
777+
<button ng-click="reset()">RESET</button>
778+
<button ng-click="update(user)">SAVE</button>
779+
</form>
780+
<pre>form = {{user | json}}</pre>
781+
<pre>master = {{master | json}}</pre>
782+
</div>
783+
</file>
784+
<file name="script.js">
785+
// Module: copyExample
786+
angular.
787+
module('copyExample', []).
788+
controller('ExampleController', ['$scope', function($scope) {
789+
$scope.master = {};
790+
791+
$scope.reset = function() {
792+
// Example with 1 argument
793+
$scope.user = angular.copy($scope.master);
794+
};
787795
788-
$scope.reset = function() {
789-
// Example with 2 arguments
790-
angular.copy($scope.master, $scope.user);
791-
};
796+
$scope.update = function(user) {
797+
// Example with 2 arguments
798+
angular.copy(user, $scope.master);
799+
};
792800
793-
$scope.reset();
794-
}]);
795-
</script>
796-
</file>
797-
</example>
801+
$scope.reset();
802+
}]);
803+
</file>
804+
</example>
798805
*/
799806
function copy(source, destination) {
800807
var stackSource = [];

0 commit comments

Comments
 (0)