I'm using AngularJS on a web project and I am noticing that almost all of my form controllers are looking the same. The only difference with the login controller (shown below) and say my reset password controller is $scope.loginForm.$invalid
would be $scope.resetForm.$invalid
and I would inject and use the ResetService
instead of the AuthService
.
angular.module('app').controller('LoginCtrl', function ($scope, AuthService) {
// Form input data
$scope.formData = {};
// Are we in the middle of a submit process?
$scope.busy = false;
// Has the form been submitted yet?
$scope.submitted = false;
// Attempt to submit form via AJAX
$scope.submit = function (actionUrl) {
$scope.busy = true;
$scope.submitted = true;
// Invalid, activate form and return
if ($scope.loginForm.$invalid) {
$scope.busy = false;
return;
}
// Submit data via AJAX
AuthService.login(actionUrl, $scope.formData).error(function () {
$scope.busy = false;
});
};
});
Obviously, this doesn't feel very DRY, and I am assuming that there is an Angular feature or pattern to extract this similar functionality out?