使用angularjs登录?

时间:2015-01-30 13:17:23

标签: javascript angularjs

我有这个处理登录的控制器:

HTML

<input id="UserName" type="text" class="form-control" name="UserName" data-ng-model="vm.userName" placeholder="username or email">

<input id="Password" type="password" class="form-control" name="Password" placeholder="password" data-ng-model="vm.password">

app.js

    var Call23 = angular.module("Call23", ["ngResource", "ngRoute", 'ui.bootstrap']).
config(function ($routeProvider) {
    $routeProvider.
        when('/', { controller: LoginCtrl, templateUrl: 'login.html' }).


        otherwise({ redirectTo: '/' });
});

   Call23.factory("page", function($rootScope){
       var page={};
       var user={};
       page.setPage=function(title,bodyClass){
           $rootScope.pageTitle = title;
           $rootScope.bodylayout=bodyClass;
       };
       page.setUser=function(user){
           $rootScope.user=user;
       }
       return page;
   });

   var LoginController = function ($scope, $routeParams, $location, LoginFactory) {
       $scope.loginForm = {
           UserName: '',
           password: '', 
           returnUrl: $routeParams.returnUrl,
           loginFailure: false,
           loginSucess: false
       };


       $scope.login = function () {

           var result = LoginFactory($scope.loginForm.UserName, $scope.loginForm.password, $scope.loginForm.rememberMe);
           result.then(function (result) {
               if (result.success) {
                   if ($scope.loginForm.returnUrl !== undefined) {
                       $location.path('/');
                   }
                   else {
                       $location.path($scope.loginForm.returnUrl);
                   }
                   $scope.loginForm.loginSucess = true;
               } else {
                   $scope.loginForm.loginFailure = true;
               }
          });
     }
  }
var LoginFactory = function ($http, $q) {
return function (UserName, Password) 
    var deferredObject = $q.defer();
    $http.post(
       'login.html', {
            Email: UserName,
            Password: Password,
        }
    ).
    success(function (data) {
        if (data == "True") {
            deferredObject.resolve({ success: true });
        } else {
            deferredObject.resolve({ success: false });
        }
    }).
    error(function () {
        deferredObject.resolve({ success: false });
    });
    return deferredObject.promise;
}
}
LoginFactory.$inject = ['$http', '$q'];

在我尝试登录或构建项目之前,我收到此错误。 你能帮忙吗?

错误:[$ injector:unpr]未知提供者:LoginFactoryProvider&lt; - LoginFactory

1 个答案:

答案 0 :(得分:2)

您还没有将LoginFactory注册为Angular提供程序,如果它是一个有效的角度服务/提供者/工厂,依赖注入就会起作用。

如果使用以下代码注册函数,则应找到LoginFactoryProvider。

angular.module('UserLogin', [])
    .provider('LoginFactory', LoginFactoryProvider);

function LoginFactoryProvider ( /* NO-DI */ ) {
    return {
        $get: ['DI', function ($DI) {
            return {
                 setLogin: function(user, pass) { /* etc */ }
            }
        }
    }
}

现在,您可以根据需要在所有控制器中添加依赖注入的LoginFactory.setLogin函数:

var LoginController = function ($scope, $routeParams, $location, LoginFactory) {

在此之前,您需要将模块UserLogin注册到app.js的第一部分,如下所示:

var Call23 = angular.module("Call23", ["ngResource", "ngRoute", 'ui.bootstrap', "UserLogin"])