angular.module('app', [])
.controller('ctrl1', function($scope) {
function clearCopy(scope) {
var dest = {};
for (var i in scope) {
if (scope.hasOwnProperty(i) && i[0] !== '$' && i !== 'this') {
dest[i] = scope[i];
}
}
return dest;
}
$scope.a = "Test";
$scope.b = {
x: 1,
y: 2
};
$scope.c = 99;
$scope.copy1 = clearCopy($scope);
})
.controller('ctrl2', function($scope) {
function clearCopy(scope) {
var internalProperiesMap = {
$$ChildScope: true,
$$childHead: true,
$$childTail: true,
$$listenerCount: true,
$$listeners: true,
$$nextSibling: true,
$$prevSibling: true,
$$watchers: true,
$$watchersCount: true,
$id: true,
$parent: true
},
dest = {};
for (var i in scope) {
if (scope.hasOwnProperty(i) && !internalProperiesMap[i]) {
dest[i] = scope[i];
}
}
return dest;
}
$scope.a = "Test";
$scope.b = {
x: 1,
y: 2
};
$scope.c = 99;
$scope.copy2 = clearCopy($scope);
}).controller('ctrl3', function($scope) {
function clearCopy(scope) {
var internalProperiesMap = {
$$ChildScope: true,
$$childHead: true,
$$childTail: true,
$$listenerCount: true,
$$listeners: true,
$$nextSibling: true,
$$prevSibling: true,
$$watchers: true,
$$watchersCount: true,
$id: true,
$parent: true
};
return Object.keys(scope).reduce(function(acc, el) {
if (el[0] !== '$' && typeof scope[el] !== "function") {
acc[el] = scope[el];
}
return acc;
}, {});
}
$scope.a = "Test";
$scope.b = {
x: 1,
y: 2
};
$scope.c = 99;
$scope.fun = function(d){return d;};
$scope.copy3 = clearCopy($scope);
console.log($scope.copy3);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.js"></script>
<div ng-app='app'>
<div ng-controller='ctrl1'>copy 1: {{copy1}}</div>
<div ng-controller='ctrl2'>copy 2: {{copy2}}</div>
<div ng-controller='ctrl3'>copy 3: {{copy3}}</div>
</div>