angular.copy
by Alejandro M
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<body ng-app="miApp" ng-controller="MainCtrl">
<form name="editUserForm">
<input type="text" ng-model="user.firstName" />
<input type="text" ng-model="user.lastName" />
<input type="button" value="Reset 1" ng-click="testReset()" />
<input type="button" value="Reset 2" ng-click="reset()" />
</form>
</body>
JavaScript
var app = angular.module('miApp', []);
app.controller('MainCtrl', function($scope) {
$scope.user = {
firstName: "Scott",
lastName: "Allen"
};
//fail
var testCopy = $scope.user;
$scope.testReset = function() {
$scope.user = testCopy;
//$scope.editUserForm.$setPristine();
}
//success
$scope.originalUser = angular.copy($scope.user);
$scope.reset = function() {
$scope.user = angular.copy($scope.originalUser);
//$scope.editUserForm.$setPristine();
};
});