1

I am trying to create an authentication module and when I try to inject my Authentication service, it fails to load that module.

Here's my code - AuthenticationController.js

define(['app',"../services/AuthenticationService"], function (app) {
app.controller('AuthController',["$scope","AuthService", function ($scope, AuthService) {
    $scope.login = function () {
        var promise = AuthService.login($scope.email, $scope.password);
        promise.then(function (result) {
            alert("successful!");
            console.log(result);
        }).catch(function (error) {
            console.log(error);
        });
    }
}]);
});

AuthenticationService.js

define(['app'], function (app) {
app.factory('AuthService', function ($http) {
    var AuthService = {
        login: function (email, password) {
        var url = "http://localhost:3000/auth/login";
        var def = $q.defer();
        $http({
            method: 'POST',
            url: url,
            data: {
                "email": email,
                "password": password
            }
        }).then(function (data) {
            if (data.data.success) {
                def.resolve();
            } else {
                def.reject(data.data.error);
            }
        }, function (error) {
            def.reject(error);
        });
        return def.promise;
    }
};
    return AuthService;
});
});

The error that I get is -

Error: ng:areq Bad Argument Argument 'AuthController' is not aNaNunction, got undefined

App.js

define(['angularAMD', 'angular-route'], function (angularAMD) {
var app = angular.module("amazonfresh", ['ngRoute']);

app.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when("/index", angularAMD.route({
            templateUrl: '/partials/login.html', controller: 'AuthController', controllerUrl: './controllers/AuthController'
        }))
        .otherwise({redirectTo: "/index"});
    $locationProvider.html5Mode(true);
});
return angularAMD.bootstrap(app);
});

When I remove the dependency of AuthService from AuthController, the code works.

Pratik
  • 695
  • 2
  • 11
  • 29

0 Answers0