2

所以,我有一个用 Angular 编写的应用程序。它本质上是一个 SPA,除了我的登录是通过传统的非 ajax 请求完成的。我想要的只是在服务器返回 401 时将页面(完全绕过路由)重定向到 /logout。我可以设置基于 ajax 的登录,但坦率地说,我没有看到好处。考虑到这一点,如果这样做更容易——我会做任何能让我克服这个非常烦人的障碍的事情,这对于 jQuery 来说非常简单。当我删除授权 cookie 时,这让我陷入了无限循环:

var manageModule = angular.module('manageModule', ['ngResource']);

manageModule.config(function($httpProvider, $routeProvider, $locationProvider) {

        $httpProvider.defaults.headers.put['Content-Type'] =
            'application/x-www-form-urlencoded';
        $httpProvider.defaults.headers.post['Content-Type'] =
            'application/x-www-form-urlencoded';


        $locationProvider.html5Mode(false);


        $httpProvider.interceptors.push(function($q, $window) {
            return {
                'responseError': function(rejection) {
                    var status = rejection.status;
                    if (status == 401) {
                        console.log('401 inside if');
                        $window.location.href = contextPath + '/logout';
                    }
                    return $q.reject(rejection);
                }
            }
        });


        $routeProvider
            .when('/majors', {
                templateUrl: contextPath+'/manage/majors',
                controller: ManageMajorsController
            })
            .when('/users', {
                templateUrl: contextPath+'/manage/users',
                controller: ManageUsersController
            })
            .when('/courses', {
                templateUrl: contextPath+'/manage/courses',
                controller: ManageCoursesController
            })
            .when('/notes', {
                templateUrl: contextPath+'/manage/notes',
                controller: ManageNotesController
            })
            .when('/manage', {
                templateUrl: contextPath+'/manage/majors',
                controller: ManageMajorsController
            });

});

感谢您的任何帮助,您可以提供!

这似乎是一种新行为,我记得它处于重定向循环中。无论哪种方式,这都是无限输出到控制台的内容。

Error: 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,\nc.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 15; oldVal: 14"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,\nc.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 16; oldVal: 15"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,\nc.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 17; oldVal: 16"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,\nc.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 18; oldVal: 17"],["fn: function (){var a=d.url(),b=f.$$replace;if(!m||a!=f.absUrl())m++,\nc.$evalAsync(function(){c.$broadcast(\"$locationChangeStart\",f.absUrl(),a).defaultPrevented?f.$$parse(a):(d.url(f.absUrl(),b),i(a))});f.$$replace=!1;return m}; newVal: 19; oldVal: 18"]]
    at Error (<anonymous>)
    at Object.e.$digest (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:91:135)
    at Object.e.$apply (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:92:431)
    at j (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:101:80)
    at r (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:104:449)
    at XMLHttpRequest.v.onreadystatechange (http://localhost:8080/MetroCredit/static/bundle-main_defer.js:106:90) bundle-main_defer.js:63
Uncaught Error: 10 $digest() iterations reached. 
4

1 回答 1

0

可能与这个问题有关。

当我根本不想使用 Angular 的路由时,我以前在我的控制器中使用过这个:

// Abort Angular JS default location change handling.
$scope.$on('$locationChangeStart', function(event, newUrl, oldUrl) {
  if (newUrl == $location.absUrl()) {
    event.preventDefault();
  }
});

对于您的情况,您似乎只想摆脱某些 URL,因此您可能希望/需要做一些模式匹配来代替newUrl == $location.absUrl().

这可能不适合您,因为我想避免使用 Angular 的所有路由机制,而且我不确定它与新版本的兼容性如何。我已经将它与 Angular 1.0.x 版本一起使用。

于 2013-06-11T23:41:39.893 回答