0

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.

2

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

there are many ways to do it, I am trying to achieve it in angular js. why angular.copy function is not working for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.