AngularJS 路由未在页面加载时解析

AngularJS route not resolved on page load

我尝试在我的应用程序中实现一个 AngularJS 路由器。 所以我定义了一条路线(在 angular.module('MyModule').config() 中):

$routeProvider
  .when('/:stepId?', {
          templateUrl: EditorApp.webDir + 'bundles/innovapath/angularjs/Step/Partial/step-form.html',
          controller: 'StepFormCtrl',
          controllerAs: 'stepFormCtrl',
          resolve: {
              step: [
                  '$route',
                  'PathService',
                  function($route, PathService) {
                      var step = null;

                      // Retrieve the step from route ID
                      if ($route.current.params && $route.current.params.stepId) {
                          step = PathService.getStep($route.current.params.stepId);
                      }

                      return step;
                  }
              ],
              inheritedResources: [
                  '$route',
                  'PathService',
                  function($route, PathService) {
                      var inherited = [];

                      var step = PathService.getStep($route.current.params.stepId);
                      if (angular.isDefined(step) && angular.isObject(step)) {
                          var path = PathService.getPath();

                          // Grab inherited resources
                          inherited = PathService.getStepInheritedResources(path.steps, step);
                      }

                      return inherited;
                  }
              ]
          }
    })
    .otherwise({
        redirectTo: '/:stepId?'
    });

我也在监听 $routeChangeStart 事件,但它没有在我的页面加载时触发。

我想不通为什么路由没有解析。此外,当我用 F5 刷新页面时,我遇到了同样的问题。

当我在我的应用程序中使用内部 link 时,路由解析完美无缺。

有什么想法吗?

好的,找到了。

在处理指令 ng-view 之前,我有很多其他需要初始化的父指令,因此它推迟了路线的计算,错过了 $locationChange 事件。

解决方案是强制路由计算:

$route.reload();