简体   繁体   English

Angular应用程序无法加载初始页面

[英]Angular app does not load initial page

In an Angular app that I'm currently developing, the first page call does not cause the correct route template to be loaded (only the menu bar is shown). 在我当前正在开发的Angular应用程序中,首页调用不会导致加载正确的路由模板(仅显示菜单栏)。 Only if I click on another link and back to the original, the correct page loads. 仅当我单击另一个链接并返回原始链接时,才会加载正确的页面。

Is there any known issue with first page loads or what can I do to debug this one? 首页加载是否存在任何已知问题,或者该如何调试?

A bit of context: I'm using Angularjs (v1.2.0-rc.2) and requirejs (and many more, but they should be irrelevant). 有一点上下文:我正在使用Angularjs(v1.2.0-rc.2)和requirejs(还有更多,但它们应该无关紧要)。 I can't provide a minimal working example, as I have no idea what to remove and what now. 我无法提供一个最低限度的工作示例,因为我不知道要删除什么以及现在要删除什么。 I also had this problem not occurring for some pages for a while, but now (after some more development on other pages), these exhibit the same behaviour. 我也有一段时间没有在某些页面上出现此问题,但是现在(在其他页面上进行了更多开发之后),这些页面表现出相同的行为。 Any pointer are appreciated... 任何指针都表示赞赏...

Update 更新

As requested, an example of the route configuration. 根据要求,提供路由配置示例。 They look all fairly similar; 它们看起来都非常相似。 some static part and some ids. 一些静态部分和一些ID。

mod.config(function ($routeProvider) {
  $routeProvider.when('/some/:id/route/to/somewhere', {
    controller: 'MyController',
    templateUrl: '/template/some.template.html'
  });
});

Update 2 更新2

After a bit of stepping through the Angular code, I found that during the initial handling of a new URL in this snippet from angular.js , no handler was registered to the event $locationChangeSuccess . 逐步浏览一下Angular代码后,我发现在对angular.js此代码段中的新URL进行初始处理期间,没有向事件$locationChangeSuccess注册任何处理程序。

// update browser
var changeCounter = 0;
$rootScope.$watch(function $locationWatch() {
  var oldUrl = $browser.url();
  var currentReplace = $location.$$replace;

  if (!changeCounter || oldUrl != $location.absUrl()) {
    changeCounter++;
    $rootScope.$evalAsync(function() {
      if ($rootScope.$broadcast('$locationChangeStart', $location.absUrl(), oldUrl).
          defaultPrevented) {
        $location.$$parse(oldUrl);
      } else {
        $browser.url($location.absUrl(), currentReplace);
        afterLocationChange(oldUrl);
      }
    });
  }
  $location.$$replace = false;

  return changeCounter;
});

return $location;

function afterLocationChange(oldUrl) {
  $rootScope.$broadcast('$locationChangeSuccess', $location.absUrl(), oldUrl);
}

Only after the processing of the first page load has happened, in angular-route.js , this line is executed $rootScope.$on('$locationChangeSuccess', updateRoute); 仅在处理完第一页加载之后,才在angular-route.js中执行该行: $rootScope.$on('$locationChangeSuccess', updateRoute); (which I believe causes the routing to work and views to display correctly). (我认为这会导致路由工作并正确显示视图)。 Every subsequent change of URL works fine... Maybe this is due to the way I initialise angular. URL的每个后续更改都可以正常工作……也许这是由于我初始化angular的方式所致。 I use: 我用:

angular.element(document).ready(function() {
  angular.bootstrap(document, ['some', 'more', 'modules', 'ngRoute']);
});

I've also updated to v1.2.5, but with no effect to my problem. 我还更新到了v1.2.5,但对我的问题没有任何影响。

This works as a workaround: 这是一种解决方法:

myApp.run(['$route', function($route)  {
  $route.reload();
}]);

Found at https://github.com/angular/angular.js/issues/1213 . https://github.com/angular/angular.js/issues/1213中找到。

try: 尝试:

mod.config(['$routeProvider', function ($routeProvider) {
  $routeProvider.when('/some/:id/route/to/somewhere', {
    controller: 'MyController',
    templateUrl: '/template/some.template.html'
  });
  $routeProvider.otherwise({
    // If you don't have any route named '/', this will be the default.
    redirectTo: '/some/route' // or '/some/route/:with/params'
  });
}]);

Another possible workaround 另一个可能的解决方法

var initApp = function () {
    angular.bootstrap(document.body, ['loginApp']);
    document.body.className += ' ng-app';
}
if ( document.body.className.indexOf('ng-app') == -1 ) {
    initApp();
}

The problem as I understand it is that the router has not been started. 据我了解,问题在于路由器尚未启动。 I have experienced the same issue. 我也遇到过同样的问题。 Including $route in app.run is enough to kickstart the router. 在app.run中包含$ route足以启动路由器。

app.run(['$rootScope', '$route', function ($rootScope, $route) {
   // Include $route to kick start the router.
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM