Angularjs Extend Usage
Angular.extend: Assigns the first-level property of the second parameter and subsequent arguments, whether simple or object, to the first-level property of the first argument, that is, if it is an object, refers to the same object and returns the first parameter object.
Instance one: var r = angular.extend (b, a); assigns the first-level property of object A (whether a simple property or an object) to the first-level property of object B, that is, if it is an object, it refers to the same object and returns object B
JS Code
var a = {
name: ' Bijian ', address
: ' Shenzhen ',
family: {
num:6,
amount: ' 80W '
}
;
var b = {};
var r = angular.extend (b, a);
Console.log (' A: ' + json.stringify (a));
Console.log (' B: ' + json.stringify (b));
Console.log (' r: ' + json.stringify (r));
b.address = ' Hanzhou ';
B.family.amount = ' 180W ';
Console.log (' A: ' + json.stringify (a));
Console.log (' B: ' + json.stringify (b));
Console.log (' r: ' + json.stringify (r));
Run Result:
Text Code
a:{' name ': ' Bijian ', ' address ': ' Shenzhen ', ' family ': {"num": 6, "Amount": "80W"}
b:{"name": "Bijian", "Address": " Shenzhen "," Family ": {" num ": 6," Amount ":" 80W "}}
r:{" name ":" Bijian "," Address ":" Shenzhen "," Family ": {" num ": 6," Amount ": 80W"}}
a:{"name": "Bijian", "Address": "Shenzhen", "Family": {"num": 6, "Amount": "180W"}
b:{"name": "Bijian", "Address": "Hanzhou", "family": {"num": 6, "Amount": "180W"}
r:{"name": "Bijian", "Address": "Hanzhou", " Family ": {" num ": 6," Amount ":" 180W "}}
Example two: var r = angular.extend (b, A, z), successively assigns the first-level attribute of object A, z, whether a simple property or an object, to the first layer of object B, that is, if it is an object, it refers to the same object and returns object B
JS Code
var a = {
name: ' Bijian ', address
: ' Shenzhen ',
family: {
num:6,
amount: ' 80W '
}
;
var z = {
family: {
amount: ' 150W ',
Mainsource: ' Operating company '
}
};
var b = {};
var r = angular.extend (b, A, z);
Console.log (' A: ' + json.stringify (a));
Console.log (' B: ' + json.stringify (b));
Console.log (' r: ' + json.stringify (r));
b.address = ' Hanzhou ';
B.family.amount = ' 180W ';
Console.log (' A: ' + json.stringify (a));
Console.log (' B: ' + json.stringify (b));
Console.log (' r: ' + json.stringify (r));
Run Result:
Text Code
a:{' name ': ' Bijian ', ' address ': ' Shenzhen ', ' family ': {"num": 6, "Amount": "80W"}
b:{"name": "Bijian", "Address": " Shenzhen "," Family ": {" Amount ":" 150W "," Mainsource ":" Operating Company "}
r:{" name ":" Bijian "," Address ":" Shenzhen "," Family " : {"Amount": "150W", "Mainsource": "Operating Company"}
a:{"name": "Bijian", "Address": "Shenzhen", "Family": {"num": 6, "Amount" : "80W"}
b:{"name": "Bijian", "Address": "Hanzhou", "family": {"Amount": "180W", "Mainsource": "Operating Company"}
r:{" Name ":" Bijian "," Address ":" Hanzhou "," family ": {" Amount ":" 180W "," Mainsource ":" Operating Company "}}
More examples than the source code to the simple, direct and accurate, angular.extend source is as follows:
JS Code
/**
* @ngdoc function
* @name angular.extend *
@function *
@description *
Extends the Destination object ' DST ' by copying all of the properties from the ' src ' object (s)
* to ' DST '. You can specify multiple ' src ' objects.
*
* @param {object} DST destination object.
* @param {... Object} src Source object (s).
* @returns {Object} Reference to ' DST '.
*
/function Extend (DST) {
var h = dst.$ $hashKey;
foreach (arguments, function (obj) {
if (obj!== DST) {
ForEach (obj, function (value, key) {
Dst[key] = value ;
});
}
});
Sethashkey (dst,h);
return DST;
}
Thank you for reading, I hope to help you, thank you for your support for this site!