AngularJS Built-in helper Functions angular.copy

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

The angular.copy function takes an object, array or a value and creates a deep copy of it.

angular.copy()

Example:

Objects:

let obj = {name: "vespa", occupation: "princess"};
let cpy = angular.copy(obj);
cpy.name = "yogurt"
// obj = {name: "vespa", occupation: "princess"}
// cpy = {name: "yogurt", occupation: "princess"}

Arrays:

var w = [a, [b, [c, [d]]]];
var q = angular.copy(w);
// q = [a, [b, [c, [d]]]]

At the above example angular.equals(w, q) will evaluate to true because .equals tests equality by value. however w === q will evaluate to false because strict comparison between objects and arrays is done by reference.



Got any AngularJS Question?