angularjs - How to preserve an object initial data? -
i facing weird behavior angularjs in preserving variable data got binded using scope, let me explain scenario.
i have form field values served form controller through scope variable, need compare updated values initial values while saving, kept initial data in variable , assigned variable scope.
whenever change form fields, controller variable data getting updated along scope variable. not sure whether correct behavior or not, guess scope should updated.
anyone please suggest correct behavior , how solve issue if that's correct behavior.
js bin: http://jsbin.com/kakapinuhe/edit?html,js,console,output
you know problem looking @ above jsbin link, let me know if needed clarification.
thanks,
siva
yes, expected behavior of javascript. when assign:
$scope.formdata = initdata; you not making copy of initdata variable. instead, formdata refering same memory space initdata. in short, referring same data.. if formdata change.. initdata lost.
you can fix using angular.copy():
$scope.formdata = angular.copy(initdata); here modified jsbin: http://jsbin.com/yoviyesero/edit?html,js,console,output
Comments
Post a Comment