javascript - How to copy object and create new object with existing Json object? -


i trying create new object have default values scope.

i found there copy function create new independent object.

angular.copy(source,destication)

but not working me. undefined when 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 trying use copy function. please let me know if missing anything.

cloning option. below example:

var deepclonedcopy = jquery.extend(true, {}, originaldata); var cloneddata; cloneddata= $.map(deepclonedcopy, function(el) { return el }); 

see article detailed explanation: http://heyjavascript.com/4-creative-ways-to-clone-objects/

additional answer added:

you need define $rootscope.default={}; before performing copy.

$rootscope.default={}; angular.copy($rootscope.answers,$rootscope.default); console.log($rootscope.answers); console.log($rootscope.default); 

see updated plnkr : https://plnkr.co/edit/hef6ushyfroxs33kras8?p=preview

alternatively can too:

$rootscope.default = angular.copy($rootscope.answers); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -