Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – Angular UI路由器刷新时的动态状态变为404大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在根据当前用户角色动态加载状态.但是当刷新页面时,它将转到404页面.另外在$stateChangeStart事件中,fromState.name为空.

有没有办法在点击刷新按钮之前进入状态?我应该在按下刷新之前存储状态然后使用它吗?

.state('404',{
    url: '/404',templateUrl: '404.tmpl.html',controller: function ($scope,$state,APP) {
        $scope.app = APP;

        $scope.goHome = function () {
            $state.go('default.page');
        };
    }
})

$urlRouterProvider.otherwise('/404');

….

$rootScope.$on('$stateChangeStart',function (e,toState,toParams,fromState,fromParams) {

    //fromState.name = '' on refresh
});

提前致谢!

解决方法

以下似乎有效,不确定这是否是最佳方法.在卸载事件之前保存状态,然后在$stateChangeStart中使用它.

.run(function ($rootScope,$window,authService,$http,$timeout,localStorageService) {

  $rootScope.$on('$stateChangeStart',fromParams) {

    if (authService.isLoggedIn()) {
        if (toState.name === "404" && fromState.name === '' && localStorageService.get("LAST_STATE") !== "404") {               
            authService.loadStates($stateProviderRef).then(function () {
                $state.go(localStorageService.get("LAST_STATE"),localStorageService.get("LAST_STATE_PARAMS"));
            });                
        }           
    }

  });

  window.onbeforeunload = function () {
    localStorageService.set("LAST_STATE",$state.current.name);
    localStorageService.set("LAST_STATE_PARAMS",$state.params);
    return null;
  }

})

大佬总结

以上是大佬教程为你收集整理的angularjs – Angular UI路由器刷新时的动态状态变为404全部内容,希望文章能够帮你解决angularjs – Angular UI路由器刷新时的动态状态变为404所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: