0

I have looked at various form reset examples for AngularJS, and they all more or less end up at the same thing -- do an angular.copy from the original data.

However...

When I do an angular.copy, it's almost as if it's replacing my form with [] instead of the expected original data.

Portion of the controller:

//get my data
$scope.phones = api.phones.query();

//"save" the original state
$scope.original = $scope.phones
console.log($scope.personalphones); //result: [$promise: Object, $resolved: false]

$scope.reset() = function() {
    angular.copy($scope.original, $scope.phones);
    $scope.phones.form.$setPristine();
}

Any help or ideas why this might be happening would be greatly appreciated.

2
  • Are you using a $resource when you do api.phones.query()? Commented Jul 25, 2014 at 23:04
  • Yes sir, blah.factory('api', [etc]){ return $resource('/api/v1/phone/:id', {}, {etc} } Commented Jul 25, 2014 at 23:06

1 Answer 1

1

When you use the query() method of a $resource, it immediately returns you an empty array. When the server responds with the data, this empty array is populated with the server response. A similar things happens with instance methods of a $resource.

So you need to wait for the server response before you copy the array. The array has a $promise property on it that you can use to do this.

$scope.phones = api.phones.query();
$scope.phones.$promise.then(
    function(response) { angular.copy($scope.original, $scope.phones); }
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.