AngularJS UL router redirect after login

I have a project using AngularJS with ui-router. Everything works fine next to the redirect from the login screen to preview on first boot.

The default state is http: // test / # /

For example, if the user is not logged in - http: // test / # / test / 1000 / details goes to the login page (which is supposed to be done)

Then after login, the system goes to default state "home - http: // test / # / ", but I want to go to http: // test / # / requests / 1000 / details

How do I save http: // test / # / requests / 1000 / details or stateName "to redirect after login?

I'm trying to use $stateChangeSuccess

to save the state log in $rootscope

, but the first one ( http: // test / # / requests / 1000 / details ) never gets saved.

Any ideas how to handle this?

Thank.

+3


source to share


1 answer


You can add the property 'previous' to $ state in your main app launch method.

This is how I solved the problem:



// add ui-router variables to $rootScope. Comes handy in many cases, for example setting page title
angular.module('app').run(['$rootScope', '$state', '$stateParams', addUIRouterVars]);

function addUIRouterVars($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;

    // add previous state property
    $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
        $state.previous = fromState;
    });
}

      

+7


source







All Articles