angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.56k stars 3.01k forks source link

When using $state.href inside a resolve, it returns null at first #3806

Closed dietergeerts closed 3 years ago

dietergeerts commented 4 years ago

This is a (check one box):

My version of UI-Router is:

    "@uirouter/angularjs": "1.0.3",
    "@uirouter/core": "5.0.3",
    "@uirouter/dsr": "1.0.3",
    "@uirouter/sticky-states": "1.5.0",
    "@uirouter/visualizer": "6.0.0",

Bug Report

Current Behavior:

When doing this in a resolve: (I added logs to find out what is going on)

        resolve: {
          /**
           * @function
           * @param {StateService} $state
           * @returns {string}
           */
          presentationUrl($state) {
            'ngInject';

            console.log('###', $state.href('presentation-state')); // null
            setTimeout(() => {
              console.log('###', $state.href('presentation-state')); // null
            });
            setTimeout(() => {
              console.log('###', $state.href('presentation-state')); // '/app/presentation'
            }, 1000);

            return $state.href('presentation-state'); // null
          },
        },

Note that this happens on a page reload, so my guess is that something is not yet fully hooked up?

Expected Behavior:

I expect that in a resolve, we can already use the href function. If this is not the case, then having it return a promise will be more logical.

Link to Plunker or stackblitz that reproduces the issue:

( if you want a response to your issue, provide a way to reproduce it ) ( http://bit.ly/UIR-Plunk1 ) ( https://stackblitz.com/edit/ui-router-angularjs )

This can't be reproduced in a plunker, because the reloading of a page can't be replicated in it, and as said above, it is then that the error happens.

stale[bot] commented 3 years ago

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.