当前位置: 首页 > 面试题库 >

AngularJS,$ digest错误,试图在需要登录的页面上重定向应用程序

阎啸
2023-03-14
问题内容

在请求的状态需要登录的情况下,我正在尝试重定向AngularJS应用。这是我在我的角度应用程序的“运行”方法中放置的方法:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {

  if(toState.data.requiresLogin && !User.isLoggedIn)
  {
    event.preventDefault();
    $state.go('login');
  }
});

看起来很简单,但是我遇到了摘要错误。它似乎由event.preventDefault()语句触发。我当然不能删除…

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [["fn: $locationWatch; newVal: 7; oldVal: 6"],["fn:     $locationWatch; newVal: 8; oldVal: 7"],["fn: $locationWatch; newVal: 9; oldVal: 8"],["fn: $locationWatch; newVal: 10; oldVal: 9"],["fn: $locationWatch; newVal: 11; oldVal: 10"]]
...

关于如何重构此错误以清除此错误的任何建议,或者该错误甚至意味着什么?


问题答案:

有一个有效的示例,如何重定向到登录。首先定义了4个状态-其中2个
需要 身份验证 ,第3个是 公共 (对于任何人), 并且还有 登录 状态:

$stateProvider
    // available for anybody
    .state('public',{
        url : '/public',
        template : '<div>public</div>',
    })
    // just for authenticated
    .state('some',{
        url : '/some',
        template : '<div>some</div>',
        data : {requiresLogin : true },
    })
    // just for authenticated
    .state('other',{
        url : '/other',
        template : '<div>other</div>',
        data : {requiresLogin : true },
    })
    // the log-on screen
    .state('login',{
        url : '/login',
        templateUrl : 'tpl.login.html',
        controller : 'UserCtrl',
    })

这就是$stateChangeStartdef,它 总是 重定向到login-如果需要的话…仅…
login及其public始终可用/任何人都可以使用

.run(['$rootScope', '$state', 'User', function($rootScope, $state, User)
{

  $rootScope.$on('$stateChangeStart'
    , function(event, toState, toParams, fromState, fromParams) {

    var isAuthenticationRequired =  toState.data 
          && toState.data.requiresLogin 
          && !User.isLoggedIn
      ;

    if(isAuthenticationRequired)
    {
      event.preventDefault();
      $state.go('login');
    }
  });
}])

在这里检查所有动作



 类似资料:
  • 我使用ASP. NET Core 3.1 Blazor网络组装应用程序(Visual Studio 2019创建的默认应用程序)。 我连接了ASP。NET标识用于用户管理,添加了脚手架标识项(登录、注册等) 当我单击“注册”时,应用程序会正确重定向到 但是如果我单击登录,应用程序会重定向到 这是错误的(我希望<代码>https://localhost:44349/Identity/Account/

  • 记录器文件中的日志- org.springframework.Security.Access.event.loggerlistener-安全授权失败,原因是:org.springframework.Security.Access.accessdeniedexception:访问被拒绝;通过身份验证的主体:org.springframework.security.authentication.ano

  • 问题内容: 我想设置我的网站,以便如果用户点击该页面并且他们已经登录,它将把他们重定向到主页。如果未登录,它将正常显示。由于登录代码内置在Django中,我该怎么做? 问题答案: 我假设你当前正在使用内置的登录视图, 或你网址中的类似内容。 你可以编写包含默认视图的登录视图。它将检查用户是否已经登录,如果已经登录则重定向,否则使用默认视图。 就像是: 当然可以相应地更改你的网址:

  • 问题内容: 我正在做一个简单的论坛,由一系列论坛组成,每个论坛分别代表首页,主题,postitit,登录名和用户列表页面。在某些页面上,当用户未登录时会出现一个链接。 我想要实现的是在登录后触发重定向(在RequestDispatcher上使用forward()),以便浏览器返回到用户在单击登录链接之前所在的页面。为此,我看到了两种解决方案。 第一种解决方案是使用带有登录按钮的HTML 和一个不可

  • 我有一个文件,带有一个,具有和字段,在上,它用

  • 有没有办法禁用Spring Security和登录页面的重定向。我的要求指定登录应该是导航菜单的一部分。 例子: 因此没有专门的登录页面。登录信息需要通过Ajax提交。如果发生错误,它应该返回指定错误的JSON,并使用正确的HTTP状态代码。如果验证通过,它应该返回一个200,然后javascript可以从那里处理它。 我希望这是有意义的,除非有任何更简单的方法可以通过Spring Securit