Michele Boscagin
Michele Boscagin

Reputation: 97

AngularJS error injecting module

I'm trying to develop a login with cookies check if a user is already logged or not.

In order, I include the app:

angular.module('ELOAuthentication', [
    'AuthenticationService',
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

Then the service

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };
});

Finally the AngularJS controller.

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

I receive the error:

Uncaught Error: [$injector:modulerr]

. What is wrong?

Upvotes: 2

Views: 70

Answers (1)

Zooly
Zooly

Reputation: 4787

You don't need to inject Services in your module.

app.js

angular.module('ELOAuthentication', [
    'ngRoute',
    'ngCookies',
    'angular-loading-bar'
])

LoginController.js

angular.module('ELOAuthentication').controller('LoginController', function ($rootScope, $scope, $http, AuthenticationService) {

    AuthenticationService.ClearCookie();

    $scope.init = function () {

    }

    $scope.login = function () {

    };
});

As mentioned by Sajal, don't forget to return your service object:

AuthenticationService.js

angular.module('ELOAuthentication').factory('AuthenticationService', function ($rootScope, $scope, $http) {

    var service = {};

    service.Login = function (email, password, callback) {
        var Url = '/api/user/GetLoginByEmailPassword';

        $http.then(Url, { email: email, password: password }).success(
            function (response) {
                var data = response.data
                callback(data);
            }).catch(function (error) {
                console.log('ERROR GetLoginByEmailPassword: ' + error);
            });
    }

    service.SetCookie = function (email, password) {

    };

    service.ClearCookie = function () {

    };

    return service;
});

Official documentation

Upvotes: 5

Related Questions