Deep extend in angular
extend later params data in prior params,
supports multiple objects,
takes from: http://stackoverflow.com/questions/15310935/angularjs-extend-recursive/15311842#15311842
HTML
<div ng-controller="Ctrl">
{{o1}} <hr/>
{{o2}} <hr/>
{{o3}} <hr/>
{{mad}} <hr/>
</div>
JavaScript
angular.module("myApp", [])
.controller("Ctrl", function($scope) {
var x= 0;
var extendDeep = function extendDeep(dst) {
if(!x) {x++; dst = angular.copy(dst);}
angular.forEach(arguments, function(obj) {
if (obj !== dst) {
angular.forEach(obj, function(value, key) {
if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
extendDeep(dst[key], value);
} else {
dst[key] = value;
}
});
}
});
return dst;
};
$scope.o1 = { "a": "mayank", "b": { "c": "eats", "d": "healthy" }, "c": "dixit" };
$scope.o2 = { "a": "mayankkkkkk", "p": "food", "q": "lives", "b": { "e": "general", "d": [1,2,3,4,5, {"mayank": "sample"}] }, "c": "people" };
$scope.mad = extendDeep($scope.o1, $scope.o2);
x = 0;
});