AngularJS:无需完全重新加载控制器即可更改哈希和路由 [英] AngularJS: Change hash and route without completely reloading controller

查看:32
本文介绍了AngularJS:无需完全重新加载控制器即可更改哈希和路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器,路径如下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆