Promise dependency resolution order in angular ui-router

前端 未结 4 908
野性不改
野性不改 2020-12-15 06:11

I have set up a top-level controller that is instantiated only when a promise (returned by a Config factory) is successfully resolved. That promise basically do

4条回答
  •  时光说笑
    2020-12-15 06:54

    Resolve objects run in parallel, and as such doesn't follow a certain hierarchy. However, nested resolves are possible by defining a higher order resolve object as a dependency to your secondary resolves.

    $stateProvider
      .state('topState', {
        url: '/',
        resolve: {
          mainResolveObj: ['$someService', function ($someService) {
            return 'I am needed elsewhere!';
          }]
        }
      })
      .state('topState.someOtherState', {
        url: '/some-other-place',
        resolve: {
          someOtherResolveObj: ['mainResolveObj', function (mainResolveObj) {
            console.log(mainResolveObj); //-> 'I am needed elsewhere!'. 
          }]
        }
      });
    

    Kind of a bad example, but I take it you get the gist of it. Define the name of a higher level resolve object as a dependency to your lower level resolve and it'll wait for it to resolve first.

    This is how we've solved preloading certain data before lower order resolve objects, aswell as authentication requirements (among other things).

    Good luck.

提交回复
热议问题