在我的AngularJS应用程序中,我有以下路由机制:
$routeProvider.when('/questions',
route.resolve('questions',
'questions/',
'questions',
'overview',
access.authorizedAccess))
这样可以正常工作,点击像domain.com/app/#/questions
这样的网址加载正确的控制器和模板。
我想要做的是选择将参数添加到当前URL而不触发页面重新加载(基本上重新解释URL)。我现在遇到的问题是通过执行window.location.hash = '#/questions?query=test';
重新解释路由导致页面重新加载。
我试图做的是:
$rootScope.$on("$locationChangeStart", function (event, next, current) {
if (next.indexOf('?') > -1)
event.preventDefault();
});
这确实不会触发路由重新解释,但它会从我的哈希中删除参数,将其重新转换为'/questions'
。