angularjs中的重定向状态

编程入门 行业动态 更新时间:2024-10-13 06:13:26
本文介绍了angularjs中的重定向状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是状态配置:

angular .module('grabhutApp', [...]) .config(function ($stateProvider, $urlRouterProvider) { $stateProvider // ACCOUNT .state('account', { abstract: true, url: '/account', templateUrl: 'index.html' }) .state('account.main', { url: '', templateUrl: 'views/account/account.login.html', controller: 'AccountController' }) . . . // NO VIEWS .state('nv', { abstract: true, url: '/nv' }) .state('nv.logout', { url: '/logout' }) });

nv 及其子状态将没有物理视图或控制器.我希望它们充当调用某些功能的链接.

The nv and its sub states will have no physical views or controllers. I want them to serve as links that calls certain functions.

调用注销方法的服务:

angular.module('grabhutApp') .factory('$grabhutAccountService', function ($state, $grabhutDataService) { var methods = { . . logoutUser: function () { $grabhutDataService.user.removeSession(); $state.go('account.main', {}, {location: 'replace'}); } . . }; return methods; });

然后是一个用于注销的按钮/链接:

<a ui-sref="nv.logout" class="button icon icon ion-log-out button-large" menu-close></a>

我想要发生的是,当状态 nv.logout 被触发$grabhutAccountService.logoutUser() 必须被调用并且必须重定向到 'account.main'

What I want to happen is that, when state nv.logout was triggered the $grabhutAccountService.logoutUser() must be called and must redirect to 'account.main'

这是我到目前为止所做的:我尝试在 nv.logout 中使用 resolve

Here is what I've done so far: I tried to use resolve in nv.logout

.state('nv.logout', { url: '/logout', resolve: { logout: function ($grabhutAccountService) { $grabhutAccountService.logoutUser(); } } })

服务被调用,但状态没有重定向.所以我尝试了另一种方法.我添加了一个控制器:

The service was called but state did not redirect. So I tried another way. I added a controller:

.state('nv.logout', { url: '/logout', resolve: { logout: function ($grabhutAccountService) { $grabhutAccountService.logoutUser(); } }, controller: function ($scope, $state) { $scope.$on('$stateChangeSuccess', function () { $state.go('account.main'); }); } })

但是 $stateChangeSuccess 没有被触发.所以我尝试使用rootScope:

But $stateChangeSuccess is not being fired. So I tried to use the rootScope:

.run(function(...., $grabhutAccountService){ . . . $rootScope.logout = function(){ $grabhutAccountService.logoutUser(); }; . . . })

然后像这样使用它:

<a ng-click="$root.logout()" class="button icon icon ion-log-out button-large" menu-close></a>

这很好用.但我很担心,因为(AFAIK)rootScope 会加载更多数据,这可能会导致操作速度变慢.此外,每当我需要像上面这样的某种功能时,我必须再次在 rootScope 中附加功能.而且我认为这不是一个好方法.顺便说一句,我正在 phonegap 中构建它,这就是内存使用如此重要的原因.

This works fine. But I'm worrying since (AFAIK) rootScope loads more data which could cause slower operation. Besides, whenever I need some kind of function like above, I would have to attach function in rootScope again. And I think that's not a good approach. BTW, I'm building this in phonegap that's why memory usage is so important.

推荐答案

哦哦,你好近啊.我重新排列了您的一些代码并得出了这个结论:

Ooooh you're so close. I rearranged some of your code and arrived at this:

app.run(function($rootScope, $grabhutAccountService) { $rootScope.$on('$stateChangeSuccess', function (evt, toState) { if (toState.name === 'nv.logout') { $grabhutAccountService.logoutUser(); $state.go('account.main'); } }); });

UI-Router 的下一个主要版本将有很多改进的钩子来做这类事情.

The next major version of UI-Router will have much improved hooks for doing this sort of thing.

更多推荐

angularjs中的重定向状态

本文发布于:2023-07-14 20:02:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   状态   angularjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!