Implementing Angular UI router lazy resolves

泪湿孤枕 提交于 2019-12-12 03:59:33

问题


I use Angular & UI router and I need for one of my parent states's resolves to be lazy.

The reason for this is that I have a state called authenticated that is inherited by a whole hierarchy of child states. I need the resolve called isAuthenticated to be resolved lazily (i.e. each time a child of the authenticated state is entered).

Here is my parent authenticated state:

    .state('authenticated', {
        abstract: true,
        parent: 'root',
        views: {
            'header@': {
                controller: 'NavbarCtrl',
                templateUrl: 'app/navbar/views/navbar.view.html'
            }
        },
        resolve: {
            currentMember: ['domainService', function (domainService) {
                return domainService.currentMember();
            }],
            isAuthenticated: ['$rootScope', '$q', '$cookies', function ($rootScope, $q, $cookies) {
                return ($rootScope.globals.authenticated && $cookies.globalsAuthenticated) || $q.reject({unAuthorized: true});
            }]
        }
    })

I want to do that so that I can redirect unauthenticated users to login page easily.

How can I have a lazy resolve using the current implementation of the UI router (0.2.15)?

P.S. I had a look at oc lazy load but is appears to be aimed at loading whole modules or files...


回答1:


you can use stateChangeStart it will help you for doing the exact thing you are looking for , check this link out

http://brewhouse.io/blog/2014/12/09/authentication-made-simple-in-single-page-angularjs-applications.html



来源:https://stackoverflow.com/questions/32423060/implementing-angular-ui-router-lazy-resolves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!