Reputation: 2377
I am trying to create new object that will have some default values from scope.
I found there is copy function to create new independent object.
angular.copy(source,destication)
But its not working for me. I get undefined when I console new object.
angular.copy($rootScope.answers,$rootScope.default);
console.log($rootScope.answers);
console.log($rootScope.default);
https://plnkr.co/edit/BFGXr7LNAe0KvQipj9JJ?p=preview
In app.js in .run method I trying to use copy function. Please let me know if I am missing anything.
Upvotes: 0
Views: 133
Reputation: 1663
Cloning is an option. Below is an example:
var deepClonedCopy = jQuery.extend(true, {}, originalData);
var clonedData;
clonedData= $.map(deepClonedCopy, function(el) { return el });
See this article to get detailed explanation: http://heyjavascript.com/4-creative-ways-to-clone-objects/
Additional answer added:
You need to define $rootScope.default={};
before performing copy.
$rootScope.default={};
angular.copy($rootScope.answers,$rootScope.default);
console.log($rootScope.answers);
console.log($rootScope.default);
See the updated plnkr : https://plnkr.co/edit/hef6uSHyFRoxs33kRAS8?p=preview
Alternatively you can do like this too:
$rootScope.default = angular.copy($rootScope.answers);
Upvotes: 2