If your AngularJS version is 1.4.0 or higher, you can take advantage of the extend
feature by using the following syntax:
angular.extend($scope.master, $scope.ownerDetails);
This will efficiently transfer all the values from $scope.ownerDetails
(or any other source of owner details) to the $scope.master
.
For versions of AngularJS below 1.4.0, there is a workaround that involves creating your own extend
method as shown in the code snippet below:
// The following code is essentially extracted from the AngularJS 1.4.0 library
function baseExtend(dst, objs, deep) {
var h = dst.$$hashKey;
for (var i = 0, ii = objs.length; i < ii; ++i) {
var obj = objs[i];
if (!angular.isObject(obj) && !angular.isFunction(obj)) {
continue;
}
var keys = Object.keys(obj);
for (var j = 0, jj = keys.length; j < jj; j++) {
var key = keys[j];
var src = obj[key];
if (deep && angular.isObject(src)) {
if (angular.isDate(src)) {
dst[key] = new Date(src.valueOf());
} else {
if (!angular.isObject(dst[key])) {
dst[key] = angular.isArray(src) ? [] : {};
}
baseExtend(dst[key], [ src ], true);
}
} else {
dst[key] = src;
}
}
}
if (h) {
dst.$$hashKey = h;
} else {
delete dst.$$hashKey;
}
return dst;
}
angular.merge = function(dst) {
var slice = [].slice;
return baseExtend(dst, slice.call(arguments, 1), true);
};