我有一个 Angular js 应用程序,它使用 ngRoute。到应用程序的重定向来自父网站,父网站会自动将一些查询参数附加到 URL 中。这会触发 Angular js 应用程序的无限循环。
父网站准备的网址如下
http://website.com/angularapp/?redirect=bla&code=anotherbla
Angular JS 获取这个 URL 并像下面这样修改它,至少这是我所看到的。
http://website.com/angularapp/?redirect=bla&code=anotherbla#/home
然后页面继续重定向到同一链接。我认为原因是 ngRoute,因为当我创建一个没有 ngRoute 的示例应用程序时,它工作得很好。
这是我的 main.js
var module = angular.module('product', []);
angular.element(document).ready(function($http) {
// There are bootstrapping functions which checks for SSO in keycloak here
});
我的ngRoute如下
module.config(function($routeProvider) {
$routeProvider.when('/home', {
templateUrl : 'pages/panels.html',
controller : 'PanelCtrl',
reloadOnSearch: false
}).otherwise({
redirectTo : '/home'
});
我认为问题是由于 ngRoute 中的 otherwise
导致的,它不断重新路由到同一位置。
解决这个问题
- I tried adding reloadOnSearch: false in route configuration
- $route.reload as soon as the bootstrapping is done
- $location.path('/home') to change the path of the url
- document.location.href to change the path of the url, the JS way
但他们都没有解决问题。
如何停止此重新加载问题?我可以添加一个过滤器,在 Angular 应用程序内部时从 url 中删除任何类型的查询参数吗?如果是的话,你能指导我该怎么做吗?
谢谢
最佳答案
您可能只想在 Keycloak 初始化后引导您的应用程序。您可以使用他们的“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();
});
});
关于javascript - Angular JS 页面加载触发无限页面加载循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30451528/