Beginning AngularJS

(WallPaper) #1
Chapter 7 ■ ServiCeS and Server CommuniCation







There is a fair bit going on here, much of which was covered in the previous chapter and some of it earlier
in this chapter. However, pay particular attention to the code shown in bold. You will see that we now ask for the
memberDataStore service when we set up our controller. Nearer to the end of the controller method, you will see the
actual call to our new memberDataStoreService service. Following (Listing 7-8) is that section of code again:


Listing 7-8. Using the memberDataStoreService


// If the registration form is valid, use the
// memberDataStoreService to submit the form data
if ($scope.registrationForm.$valid) {


var promise = memberDataStoreService.doRegistration($scope.person);


promise.success(function (data, status) {
$scope.showSuccessMessage = true;
});


promise.error(function (data, status) {
$scope.showErrorMessage = true;
});


$scope.doShow = true;
}


There is no point submitting invalid data, so we first check to make sure that the user properly completed
all of the required fields. Assuming that the user did, we can now send the data on its way, using the
memberDataStoreService.doRegistration method. Note that the argument to this method is $scope.person.
This contains the validated data captured during the form entry process.
Of course, this isn’t the end of the process, as we still have to await the outcome of our attempt to submit the
data. This attempt will either be successful or it will fail, and we cater to both possibilities, using the promise object’s
success and error methods. Both of these methods refer to some additional HTML elements that we have placed at
the top of the HTML form. Following (Listing 7-9) is that section of code again:


Listing 7-9. Honing In on the Success and Error Messages



Thank you for taking the time to register!


There appears to have been a problem with your registration.

Free download pdf