$state.go(app.location) 和 $location.path("app/location") 有什么区别?有什么区别、app、go、state

2023-09-06 08:40:37 作者:手一挥就再见

我最近开始使用 ionic 框架,里面有 angular js.为了在屏幕之间导航,我使用了 $location.path 并且效果很好.但是,在我下载的一个示例中,我看到 $state.go 被用来重定向到某个页面.我想知道两者的区别.

I have recently started using ionic framework, it has angular js in it. To navigate between screens, I was using $location.path and it's working great. However, in an example I've downloaded, I saw $state.go being used to redirect to some page. I would like to know the difference between the two.

推荐答案

$location 服务在 angular.js 框架上开箱即用,允许您管理位置对象(类似于纯 javascript 中的).$state 服务是 ui-router 模块的一部分,允许您以高级模式管理路由,贯穿视图的状态机管理.

The $location service is on the angular.js framework out of the box and allow you to manage location object (similar to that in pure javascript). The $state service is a part of ui-router module and allows you to manage routes in an advanced mode, throughout a state machine management of views.

如果你使用 ui-router,你应该更喜欢使用 $state 服务来管理状态/路由,因为状态抽象了路由的概念,你可以在不改变状态的情况下改变物理路由.

If you use ui-router, you should prefer to use $state service to manage states/routes because state abstracts the concept of route and you could change the physical routes without changing states.

除此之外,如果您在 hashbang 模式 下运行,您可能会遇到更多问题,尤其是在您的 html 链接中.在这种情况下,最好使用 ui-sref 而不是 ng-href(或只是 href).在我看来,您应该始终考虑状态而不是路径.显然,如果您知道自己在做什么,则可以混合使用这些服务

Besides that, more problems you could have if you run in hashbang mode, in particular in your html links. In this case it's preferable to use ui-sref rather than ng-href (or just href). In my opinion, you should always think in term of states rather than paths. Obviously you can mix the services if you know what you're doing