Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在运行$http请求之前触发工厂_Angularjs_Constants_Angularjs Rootscope - Fatal编程技术网

Angularjs 在运行$http请求之前触发工厂

Angularjs 在运行$http请求之前触发工厂,angularjs,constants,angularjs-rootscope,Angularjs,Constants,Angularjs Rootscope,如何在“检查用户的登录状态”功能之前启动authCheck工厂 我正在尝试检查路由和http请求的$rootScope状态: //Global Logout Function myApp.run(function($rootScope, $http) { $rootScope.logout = function() { $http.post('/api/auth/logout'); }; }); //Check Login state of user myApp.

如何在“检查用户的登录状态”功能之前启动authCheck工厂

我正在尝试检查路由和http请求的
$rootScope
状态:

//Global Logout Function
myApp.run(function($rootScope, $http) {
    $rootScope.logout = function() {
        $http.post('/api/auth/logout');
    };
});
//Check Login state of user
myApp.run(function($rootScope, $http, $window) {
    $rootScope.$on('$routeChangeStart', function () {
        $http.get('/api/auth')
        .then(function successCallback(response) {
            $rootScope.logStatus = response.data.data.loggedIn;
            console.log('initial ' + $rootScope.logStatus);
        }, function errorCallback(response) {
            $rootScope.logStatus = response.data.data.loggedIn;
        });
    return $rootScope.logStatus;
    });

});
//Check for authenticated users on http requests (API calls and Routing changes) and redirect to login if logged out
myBirkman.factory('authCheck', ['$rootScope','$window', function($rootScope, $window) {  

var authCheck = {
    'request': function(config) {
        if ($rootScope.logStatus == true) {
            //do nothing
            console.log('redirect ' + $rootScope.logStatus);
        } else if ($rootScope.logStatus == false) {
            $window.location.href = '/login.php';
        }
    },
    'response': function(response) {
return response;
    }
};
return authCheck;
}]);




// Define routing within the app
myApp.config(['$httpProvider', '$routeProvider', function($httpProvider, $routeProvider) {  
$httpProvider.interceptors.push('authCheck');

我试图将$rootScope元素转换为常量,但同样的问题也出现了。工厂在运行函数之前运行,因此常数在工厂运行之后才会更新

如果在解析承诺后填充值,则无法确定是否存在该值。您将无法获得正确的
$rootScope.logStatus
值,因为它仅在
$http.get
调用完成后才填充,这可能发生在您的工厂代码完成执行后,这要感谢Aditya。解决方案是我的拦截器函数格式不正确。重新格式化后,我的代码工作得很好。请注意,不要忘记将请求中的配置和响应中的响应都传回,以便您的请求仍能按预期运行

您是否试图在身份验证返回响应之前检查状态?如果没有,那么console.log$rootScope是的,console.log#2只是检查rootScope是否可用(用于故障排除)。我留下它是为了指出我在哪里看到rootScope作为未定义返回。请参阅以了解异步请求是如何工作的。这有什么帮助?http请求正在返回我想要的数据。我的问题是使rootScope持久化。我已经修改了代码(请参见上面的编辑),但问题是在check login函数之前启动了loggedIn状态检查。谢谢Adita,这很有意义。那么你建议我如何处理这个具体问题呢?我必须将原始http调用的逻辑与拦截器分开,因为它会导致循环逻辑循环。请改用
http拦截器
。请参阅:,我使用的是拦截器。看上面最后一行,是的。您只需使用语法为
request
requestError
response
responseError
我的道歉我以为您错过了最后一行,所以我从未看过您提供的链接。非常感谢你!我很快就会发布正确的答案。