AngularJS:在不完全重新加载控制器的情况下更改哈希和路由 [英] AngularJS: Change hash and route without completely reloading controller
问题描述
我有一个控制器,其路由如下:
I have a controller, with a route like this:
#/articles/1234
#/articles/1234
我想在不完全重新加载控制器的情况下更改路线,这样我可以保持控制器中其他东西的位置不变(滚动的列表)
I want to change the route without completely reloading the controller, so I can keep the position of other stuff in the controller constant (lists that scroll)
我可以想到几种方法来做到这一点,但它们都非常难看.有没有做这样的事情的最佳实践?我尝试使用 reloadOnSearch: false 进行试验,但它似乎没有改变任何东西.
I can think of a few ways to do this, but they're all pretty ugly. Is there a best practice for doing something like this? I tried experimenting with reloadOnSearch: false, but it doesn't seem to change anything.
推荐答案
有同样的挑战,
在另一个 StackOverflow 响应中发现了一个技巧
相当干净的解决方案 - 我所做的只是将这些行添加到设置 $location.path 的控制器中:
Fairly clean solution - all I did was to add these lines to the controller that sets $location.path:
var lastRoute = $route.current;
$scope.$on('$locationChangeSuccess', function(event) {
$route.current = lastRoute;
});
..当然要确保 $route 被注入到控制器中.
..and made sure $route in injected into the controller of course.
但仍然感觉DoNotFollowRoutesOnPathChange"是 AngularJS 中缺少的功能.
But still, feels like "DoNotFollowRoutesOnPathChange" is a missing feature in AngularJS.
/詹斯
更新:由于监听此事件有效地杀死了 $routeProvider 配置的进一步使用,我不得不将此捕获限制为仅当前路径:
Update: Since listening to this event effectively kills further usage of $routeProvider configs, I had to limit this catch to current path only:
var lastRoute = $route.current;
if ($route.current.$route.templateUrl.indexOf('mycurrentpath') > 0) {
$route.current = lastRoute;
}
变丑了……
这篇关于AngularJS:在不完全重新加载控制器的情况下更改哈希和路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!