Skip to content

Using $urlRouterProvider.when() and $stateChangeSuccess/event.preventDefault() the same time lead to infinite loop #1348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fubupc opened this issue Sep 9, 2014 · 7 comments

Comments

@fubupc
Copy link

fubupc commented Sep 9, 2014

At first I want to check authorization before state transition. So I use $stateChangeSuccess with e.preventDefault method to intercept transition, sample code:

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

        checkAuthorization().then(function() {
            $state.go(toState, toParams, {notify: false})
                .then(function() {
                    $rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
                 })
        });
})

It works fine until using $urlRouterProvider.when() for some url redirect:

$urlRouterProvider.when('/A', '/A/B');

Now if you visit url /A, then the browser would be trapped into an indefinite loop. I made a simple demo plunker here: http://plnkr.co/edit/lUkJx27HiaaNAJbF07n2?p=preview

Please open console and click 'A' link to see what I mean...

PS: This is an issue relative to #178

@nateabele nateabele added the bug label Sep 10, 2014
@lonormaly
Copy link

I wasted days trying to solve the issues revolves around the changeState...

For this issue I figured that if I add this code, it works:

        $rootScope.$on('$locationChangeStart', function (event, next, current) {
            $rootScope.$broadcast('$locationChangeSuccess', next, current);
        });

@fubupc
Copy link
Author

fubupc commented Sep 11, 2014

@lonormaly Actually I have found a workaround as well: add an $urlRouter.update(true) after e.preventDefault(). sample code:

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

        $urlRouter.update(true); // add this!

        checkAuthorization().then(function() {
            $state.go(toState, toParams, {notify: false})
                .then(function() {
                    $rootScope.$broadcast('$stateChangeSuccess', toState, toParams, fromState, fromParams);
                 })
        });
})

But it has a drawback: if the authorization check failed, the url will not revert back.

@lonormaly
Copy link

Worked for me, thanks!

Still a workaround...

@nateabele nateabele added docs and removed bug labels Sep 12, 2014
@ProLoser
Copy link
Member

I tend to just use an authorized state and make the check part of the resolve, then I simply parent it to whatever states I want to require authorization under. Resolves FTW.

@fubupc
Copy link
Author

fubupc commented Sep 20, 2014

@ProLoser Another approach for authorization. Thanks!

@gergelyhegykozi
Copy link

Maybe it can be relevant in this issue: https://github.com/leonuh/ui-router.redirect

@stale
Copy link

stale bot commented Jan 24, 2020

This issue has been automatically marked as stale because it has not had
recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues
may be reopened.

Thank you for your contributions.

@stale stale bot added the stale label Jan 24, 2020
@stale stale bot closed this as completed Feb 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants