The closing of the Immediately Invoked Function Expression (IIFE) in your code snippet is not correct. It should be:
})();
This is the correct format:
(function ( /* IIFE enclosed code within this function*/) {
...
...
})();
Additionally, you are injecting services into your configuration that should not be injected. You can use services within configuration functions without explicit injection, similar to function parameters.
Here is your code snippet following a coding standard closer to John Papa's Angular Style Guide:
(function () {
// module definition
var AAngScript = angular.module('AAngScript', ['ngRoute']);
// Add controller to angular app
angular.module('AAngScript').controller('LoginController', LoginController);
// Inject dependencies into controller
LoginController.$inject = ['$routeParams', '$location', 'LoginFactory'];
// Define login controller
function LoginController($routeParams, $location, LoginFactory){
// vm as in view-model
var vm = this;
vm.loginForm = {
emailAdress: '',
password: '',
rememberMe: false,
returnUrl: $routeParams.returnUrl,
loginFailure: false
};
vm.login = function () {
var result = LoginFactory($scope.loginForm.emailAdress, $scope.loginForm.password, $scope.loginForm.rememberMe);
result.then(function (result) {
if (result.success) {
if ($scope.loginForm.returnUrl !== undefined) {
$location.path('/productcategory');
} else {
$location.path($scope.loginForm.returnUrl);
}
} else {
$scope.loginForm.loginFailure = true;
}
});
}
}
// Add controller to angular app
angular.module('AAngScript').controller('RegisterController', RegisterController);
// Inject dependencies into controller
RegisterController.$inject = ['$location', 'RegistrationFactory'];
// Define register controller
function RegisterController ($location, RegistrationFactory) {
// vm as in view-model
var vm = this;
vm.registerForm = {
emailAdress: '',
password: '',
confirmPassword: '',
registrationFailure: false
};
vm.register = function () {
var result = RegistrationFactory($scope.registerForm.emailAdress, $scope.registerForm.password, $scope.registerForm.confirmPassword);
result.then(function (result) {
if (result.success) {
$location.path('/routeOne');
} else {
$scope.registerForm.registrationFailure = true;
}
});
}
}
// Add factory to angular app
angular.module('AAngScript').factory('AuthHttpResponseInterceptor', AuthHttpResponseInterceptor);
// Inject dependencies into factory
AuthHttpResponseInterceptor.$inject = ['$q', '$location'];
// Define AuthHttpResponseInterceptor factory
function AuthHttpResponseInterceptor($q, $location) {
var factory = this;
factory.response = function (response) {
if (response.status === 401) {
console.log("Response 401");
}
return response || $q.when(response);
}
factory.responseError = function (rejection) {
if (rejection.status === 401) {
console.log("Response Error 401", rejection);
$location.path('/login').search('retutnUrl', $location.path());
}
return $q.reject(rejection);
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('LoginFactory', LoginFactory);
// Inject dependencies into factory
LoginFactory.$inject = ['$http', '$q'];
// Define LoginFactory
function LoginFactory($http, $q) {
var factory = this;
factory.login = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Login', {
Email: emailAdress,
Password: password,
RememberMe: rememberMe
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true })
} else {
deferredObject.resolve({ success: false })
}
}).
error(function () {
deferredObject.resolve({ success: false })
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add factory to angular app
angular.module('AAngScript').factory('RegistrationFactory', RegistrationFactory);
// Inject dependencies into factory
RegistrationFactory.$inject = ['$http', '$q'];
// Define RegistrationFactory
function RegistrationFactory($http, $q) {
var factory = this;
factory.register = function(emailAdress, password, rememberMe){
var deferredObject = $q.defer();
$http.post(
'/Account/Register', {
Email: emailAdress,
Password: password,
ConfirmPassword: confirmPassword
}
).
success(function (data) {
if (data == "True") {
deferredObject.resolve({ success: true });
} else {
deferredObject.resolve({ success: false });
}
}).
error(function () {
deferredObject.resolve({ success: false });
});
return deferredObject.promise;
}
// The factory object must be returned. Failure to do so results in an injection error
// from an undefined factory.
return factory;
};
// Add configuration to angular app
angular.module('AAngScript').config(Routing);
// define configuration
function Routing($routeProvider, $httpProvider){
$routeProvider.
when('/register', {
templateUrl: 'Account/Register',
controller: 'RegisterController'
})
.when('/login', {
templateUrl: '/Account/Login.html',
controller: 'LoginController'
});
$httpProvider.interceptors.push('AuthHttpResponseInterceptor');
}
})();