简体   繁体   English

Angular JS页面加载触发无限页面加载循环

[英]Angular JS page load triggers infinite page load loop

I have an angular js app, which using ngRoute. 我有一个使用ngRoute的有角度的js应用程序。 The redirection to the application is coming from a parent website and the parent website appends some query parameters into the URL automatically. 到应用程序的重定向来自父网站,并且父网站会自动将一些查询参数附加到URL中。 This triggers an infinite loop of the angular js application. 这将触发angular js应用程序的无限循环。

The url that is prepared by the parent website is like 父网站准备的网址如下

http://website.com/angularapp/?redirect=bla&code=anotherbla

Angular JS takes this URL and modifies it like below, atleast thats what i have seen. Angular JS使用此URL并按如下所示对其进行修改,这至少就是我所看到的。

http://website.com/angularapp/?redirect=bla&code=anotherbla#/home

And then the page keeps on redirecting to the same link. 然后页面继续重定向到相同的链接。 I think the reason is because of ngRoute, because when i created a sample app without ngRoute it worked just fine. 我认为原因是因为ngRoute,因为当我创建不带ngRoute的示例应用程序时,它工作得很好。

Here is my main.js 这是我的main.js

var module = angular.module('product', []);    
angular.element(document).ready(function($http) {
// There are bootstrapping functions which checks for SSO in keycloak here
});

My ngRoute is as below 我的ngRoute如下

module.config(function($routeProvider) {
    $routeProvider.when('/home', {
templateUrl : 'pages/panels.html',
        controller : 'PanelCtrl',
        reloadOnSearch: false
    }).otherwise({
    redirectTo : '/home'
});

I think the problem is because of the otherwise in ngRoute, which keep on rerouting to the same location. 我认为问题是由于ngRoute中的otherwise情况所致,它们会继续重新路由到同一位置。

To fix this 要解决这个问题

  1. I tried adding reloadOnSearch: false in route configuration 我尝试添加reloadOnSearch:路由配置中的false
  2. $route.reload as soon as the bootstrapping is done 引导完成后立即运行$ route.reload
  3. $location.path('/home') to change the path of the url $ location.path('/ home')更改URL的路径
  4. document.location.href to change the path of the url, the JS way document.location.href更改url的路径,即JS方式

But none of them is solving the issue. 但是他们都没有解决这个问题。

How do I stop this reloading issue? 如何停止此重新加载问题? Can I add a filter which will remove any sort of query parameters from the url when inside the angular application? 我可以添加一个过滤器,以便在角度应用程序内部时从网址中删除所有类型的查询参数吗? If yes, can you guide me how to do that? 如果是,您可以指导我如何做吗?

Thanks 谢谢

You might want to bootstrap your application only after Keycloak has initialized. 您可能只想在Keycloak初始化后引导您的应用程序。 You can use their "AngularJS" example as base, but basically, use this: 您可以使用其“ AngularJS”示例作为基础,但基本上,请使用以下示例:

var auth = {};
angular.element(document).ready(function ($http) {
  var keycloakAuth = new Keycloak('keycloak.json');
  auth.loggedIn = false;

  keycloakAuth.init({ onLoad: 'login-required' }).success(function () {
    auth.loggedIn = true;
    auth.authz = keycloakAuth;
    auth.logoutUrl = keycloakAuth.authServerUrl + "/realms/demo/tokens/logout?redirect_uri=/angular-product/index.html";
    module.factory('Auth', function() {
      return auth;
    });
    angular.bootstrap(document, ["product"]);
  }).error(function () {
    window.location.reload();
  });

});

https://github.com/keycloak/keycloak/blob/master/examples/demo-template/angular-product-app/src/main/webapp/js/app.js#L23 https://github.com/keycloak/keycloak/blob/master/examples/demo-template/angular-product-app/src/main/webapp/js/app.js#L23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM