Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Reset $scope.data to original values

Tags:

I've created a fiddle here: http://jsfiddle.net/nicktest2222/W4VaA/

I just want to be able to hit the reset button and put my original values back. Does anyone know the best way to go about doing this?

Thanks in advance

function TodoCtrl($scope) {   $scope.data = [    {text:'learn angular', done:true},    {text:'build an angular app', done:false}];    $scope.orig = [$scope.data];    $scope.reset = function() {     $scope.data = $scope.orig;   };  } 
like image 367
Nick Avatar asked Jul 09 '13 22:07

Nick


1 Answers

The problem is in JS clone mechanics. All you need to do is to create a deep copy of your model:

function TodoCtrl($scope) {     $scope.data = [         {text:'learn angular', done:true},         {text:'build an angular app', done:false}     ];      $scope.orig = angular.copy($scope.data);      $scope.reset = function() {         $scope.data = angular.copy($scope.orig);     }; } 

Here is the updated fiddle.

like image 107
madhead - StandWithUkraine Avatar answered Oct 21 '22 09:10

madhead - StandWithUkraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!