If you can view this plunker link, you know my exact problem. I am unable to keep an old copy of my scope variable.
Below is my code.
$scope.init = function() {
var data = [{
id: 1,
d: 'mon',
sel: false
}, {
id: 2,
d: 'fri',
sel: false
}, {
id: 3,
d: 'fri',
sel: false
}, {
id: 4,
d: 'sun',
sel: false
}];
$scope.myData = data;
$scope.oldMyData = data;
angular.forEach($scope.myData, function(value, key) {
if(value.d == 'sun'){
value.sel = true;
}
}, $scope.myData);
Here the oldMyData
is also getting updated. How do I preserve a copy of the old data ?
p.s : I am not using jquery.
var original;
original = angular.copy($scope.myData);
$rootScope.original = angular.copy($scope.myData); // this original variable can access inside your angular app
dont forget to inject $rootScope to the controller
you have the the old values in var original
:)