AngularJS App的Python Tornado服务和从AngularApp返回服务器的初始路由

时间:2013-12-12 13:21:14

标签: angularjs tornado

我遇到的问题是,应该由ng-route处理的初始href请求被路由到后端服务器。在初始请求之后,相同的href将使用我的路由配置在我的应用程序中正确路由。

我的app / index.html由Tornado后端服务器提供服务,该服务器处理初始(非spa)登录页面和多平台登录支持。经过身份验证后,我的龙卷风服务器会编写app / index.html。角度如何决定何时路由到服务器以及何时不到?关于在哪里调试这个的任何建议?

1 个答案:

答案 0 :(得分:0)

初始化角度应用程序和设置路线后,angular就可以为客户端路线提供服务。

//index.html page
app.config( function ( $routeProvider,$locationProvider ) {
  $routeProvider
    .when( '/this', { templateUrl: 'this.html' } )
    .when( '/that', { templateUrl: 'that.html' } )
    .when( '/other', { templateUrl: 'other.html' } )
    .otherwise( { redirectTo: 'this' } );

     $locationProvider.html5Mode(true);
});


app.controller( 'MainCtrl', function ( $scope, $location ) {
    $scope.location = $location;
});

具体取决于您设置路由链接的方式,如:

<div ng-controller="MainCtrl">
    <div>{{location.path()}}</div>
  <ul>
    <li><a href="#/this">This</a></li>
    <li><a href="#/that">That</a></li>
    <li><a href="#/other">Other</a></li>
  </ul>
  <ng-view></ng-view>
</div>

将是客户端路线(小提琴示例)。

如果启用:

$locationProvider.html5Mode(true);

然后你可以做类似的事情:

<ul>
 <li><a href="this">This</a></li>
 <li><a href="that">That</a></li>
 <li><a href="other">Other</a></li>
</ul>

是客户端路线(fiddle example 2)

Angular $route Documentation