Jaspreet Singh
Jaspreet Singh

Reputation: 327

catch exception in angularJS

I just went through an angular-fullstack application and i came across this piece of code:

catch( function(err) {
      err = err.data;
      $scope.errors = {};

      // Update validity of form fields that match the mongoose errors
      angular.forEach(err.errors, function(error, field) {
        form[field].$setValidity('mongoose', false);
        $scope.errors[field] = error.message;
      });

I understand the piece of code what it is trying to say but i want to know if suppose an error occur what exactly is passed to function(error, field). i unable to interpret what happens if error occurred. so that i'll able to know what is actually happening in this code

This piece of code is in a controller

Can anyone please explain the whole procedure with example?

Upvotes: 0

Views: 614

Answers (1)

David L
David L

Reputation: 33823

Looks like you're "catching" errors (probably returned from a restful service) and mapping each error in the error array to it's specific field.

The validity of that field is then set to false, which is something held by the angular form.

Finally, there is some sort of binding to $scope.errors that displays each error message that is added to the $scope.errors array.

Looks like pretty simple and typical validation. It has nothing to do with core angular error handling and is simply a way to add validation information to the form/page.

Upvotes: 1

Related Questions