Javascript 将此$rootScope转换为http基本身份验证angularjs中使用的工厂

Javascript 将此$rootScope转换为http基本身份验证angularjs中使用的工厂,javascript,angularjs,basic-authentication,angularjs-factory,angularjs-rootscope,Javascript,Angularjs,Basic Authentication,Angularjs Factory,Angularjs Rootscope,我正在使用这个示例使用angularjs构建一个HTTP基本身份验证应用程序。 编写器使用$rootScope存储用户名和密码。我想将其转换为使用factory,尤其是当示例已经使用factory时 以下是相关代码 .factory('AuthenticationService', ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout', function (Base64, $http, $cookieStore

我正在使用这个示例使用angularjs构建一个HTTP基本身份验证应用程序。

编写器使用$rootScope存储用户名和密码。我想将其转换为使用factory,尤其是当示例已经使用factory时

以下是相关代码

.factory('AuthenticationService',
    ['Base64', '$http', '$cookieStore', '$rootScope', '$timeout',
    function (Base64, $http, $cookieStore, $rootScope, $timeout) {
        var service = {};

        service.Login = function (username, password, callback) {

            /* Dummy authentication for testing, uses $timeout to simulate api call
             ----------------------------------------------*/
            $timeout(function(){
            var response = { success: username === 'test' && password === 'test' };
            if(!response.success) {
                response.message = 'Username or password is incorrect';
            }
            callback(response);
        }, 1000);


        /* Use this for real authentication
         ----------------------------------------------*/
        //$http.post('/api/authenticate', { username: username, password: password })
        //    .success(function (response) {
        //        callback(response);
        //    });

    };

    service.SetCredentials = function (username, password) {
        var authdata = Base64.encode(username + ':' + password);

        $rootScope.globals = {
            currentUser: {
                username: username,
                authdata: authdata
            }
        };
    return service;
}]);

angular.module('BasicHttpAuthExample', [
    'Authentication',
    'Home',
    'ngRoute',
    'ngCookies'
]).run(['$rootScope', '$location', '$cookieStore', '$http',
    function ($rootScope, $location, $cookieStore, $http) {
        // keep user logged in after page refresh
        $rootScope.globals = $cookieStore.get('globals') || {};
        if ($rootScope.globals.currentUser) {
            $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata; // jshint ignore:line
        }

        $rootScope.$on('$locationChangeStart', function (event, next, current) {
            // redirect to login page if not logged in
            if ($location.path() !== '/login' && !$rootScope.globals.currentUser) {
                $location.path('/login');
            }
        });
    }]);

那么你的问题是什么?如果你想把它转换成使用工厂,那就去吧。当你有具体问题时再来。那么你的问题是什么?如果你想把它转换成使用工厂,那就去吧。有特殊问题时请回来。