AngularJS:更改哈希和路线不完全重装控制器 [英] AngularJS: Change hash and route without completely reloading controller
问题描述
我有一个控制器,具有这样的路线:
I have a controller, with a route like this:
#/用品/ 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)
我能想到的几个方法可以做到这一点,但他们都pretty难看。有没有做这样的事情最好的做法?我试着用reloadOnSearch试验:假的,但它似乎并没有改变任何东西。
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.
推荐答案
有同样的challange,
Had the very same challange,
还算干净的解决方案 - 我所做的就是将这些行添加到设置$ 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;
});
..并确保$路线在注入过程中的控制器。
..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 CONFIGS进一步使用,我不得不限制这种捕捉到目前唯一的路径:
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屋!