简体   繁体   English

AngularJS - 如何让服务器端和客户端路由协同工作

[英]AngularJS - How to get serverside and clientside routing to work together

Im working on porting a project over to an angular based SPA. 我正致力于将项目移植到基于角度的SPA。 Its currently a more "traditional" node/locomotivejs app that serves up templates from the server side (never known the proper term for this). 它目前是一个更“传统”的节点/ locomotivejs应用程序,它从服务器端提供模板(从来不知道适当的术语)。

The projects too large to to migrate all at once, so we are converting it to angular a page at a time. 项目太大而无法一次性迁移,因此我们一次将其转换为一个页面。

My problem: if you load the angular part of the app, everything works fine. 我的问题:如果你加载应用程序的角度部分,一切正常。 You can go to the angular routes correctly. 你可以正确地去角度路线。 However if you then go to a non-angular route (that should be handled serverside), then nothing loads (the ng-view goes blank, rather than a whole new template being loaded up). 但是,如果您转到非角度路径(应该在服务器端处理),则不会加载任何内容(ng-view变为空白,而不是加载整个新模板)。 If you go to a serverside route first or hit refresh, the page loads correctly. 如果您首先转到服务器端路由或点击刷新,页面将正确加载。

My guess is that angular is trying to handle these routes, and i am unsure how to get it to let the server take back over. 我的猜测是angular正试图处理这些路由,我不确定如何让它让服务器收回来。

app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
  $locationProvider.html5Mode(true);
  $routeProvider.when('/something/page1', {
    templateUrl: '/page1.html',
    controller: 'page1Ctrl'
  });
  $routeProvider.when('/something/page1/subpage', {
    templateUrl: '/subpage.html',
    controller: 'subpageCtrl'
  });
}]);

this is my angular routeProvider. 这是我的角度路线提供者。 No "otherwise" specified. 没有“其他”指定。 Serverside I have something like: Serverside我有类似的东西:

this.match( '/someOtherPage', 'someOtherPage#showstuff');

If i go to /someOtherPage directly, it loads correctly from the serverside. 如果我直接转到/ someOtherPage,它会从服务器端正确加载。 If i go to /something/page1, then go to /someOtherPage, it does not seem to contact the server. 如果我去/ something / page1,然后转到/ someOtherPage,它似乎没有联系服务器。

Because you are using angular html 5 mode angular cannot tell the difference between a route that you want angular to handle, and one you don't. 因为你使用的是角度html 5模式,角度不能区分你想要角度处理的路线和你不想要角度的路线。 I think you need to tell angular to ignore certain routes. 我认为你需要告诉angular忽略某些路线。 Looks like this is what you are looking for: 看起来这就是你要找的东西:

https://docs.angularjs.org/guide/$location#html-link-rewriting https://docs.angularjs.org/guide/$location#html-link-rewriting

So change your links to non-angular pages to use a target . 因此,将链接更改为非角度页面以使用target

ex. 恩。 <a href="/ext/link?a=b" target="_self">link</a>

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

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