向URL添加参数时阻止路由解释

时间:2013-09-04 08:59:43

标签: javascript angularjs

在我的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'

1 个答案:

答案 0 :(得分:2)

您可以在初始化reloadOnSearch:false中的路线时尝试选项$routeProvider。请参阅文档here