与Google App Engine和Flask一起使用时,Angular路由无法正常工作

vic*_*icg 10 python google-app-engine flask angularjs angularjs-routing

我正在做的事情:

我正在尝试在Google App Engine上构建RESTful Flask应用程序,使用Angular处理路由和视图逻辑,而Flask处理后端逻辑和资源.

问题:

当我为GAE启动开发服务器时,第一页完全加载.问题是,当我单击页面顶部的"引荐"链接时,正在加载的模板不会更改.

到目前为止我做了什么

虽然看起来我下面粘贴了很多代码,但大部分代码都是标记,并且实际上并没有任何复杂的应用程序逻辑,所以略读是足够的

我计划先建立前端,然后是后端(虽然我已经有了一些后端设置).该应用程序目前不依赖于烧瓶应用程序(它没有任何应用程序逻辑,也没有任何请求处理程序)

这是我的app.js文件,到目前为止我所做的只是路由,没有逻辑:

// app.js, only angular code in project and only does routing
var rcsApp = angular.module('rcsApp', [
    'ngRoute'
]);

rcsApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'templates/welcome-page.html'
            }).
            when('/index', {
                templateUrl: 'templates/welcome-page.html'
            }).
            when('/referrals', {
                templateUrl: 'templates/referrals.html'
            }).
            when('/404', {
                templateUrl: 'templates/404.html'
            }).
            otherwise({
                redirectTo: '/404'
            });

}]);
Run Code Online (Sandbox Code Playgroud)

这是我的app.yaml文件,这是我用来提供静态页面的内容

# This file specifies your Python application's runtime configuration
# including URL routing, versions, static file uploads, etc. See
# https://developers.google.com/appengine/docs/python/config/appconfig
# for details.

# TODO: Enter your application id below. If you have signed up
# using cloud.google.com/console use the "project id" for your application
# id.
application: placeholder
version: 1
runtime: python27
api_version: 1
threadsafe: yes

# Handlers define how to route requests to your application.
handlers:
# App Engine serves and caches static files contained in the listed directories
# (and subdirectories). Uncomment and set the directory as needed.
#- url: /client
#  static_dir: client

- url: /css
  static_dir: static/css

- url: /img
  static_dir: static/img

- url: /js
  static_dir: static/js

- url: /templates
  static_dir: templates

- url: /api/.*  
  script: main.app

- url: .*
  static_files: templates/app-view-wrapper.html
  upload: templates/app-view-wrapper.html

# Third party libraries that are included in the App Engine SDK must be listed
# here if you want to use them.  See
# https://developers.google.com/appengine/docs/python/tools/libraries27 for
# a list of libraries included in the SDK.  Third party libs that are *not* part
# of the App Engine SDK don't need to be listed here, instead add them to your
# project directory, either as a git submodule or as a plain subdirectory.
# TODO: List any other App Engine SDK libs you may need here.
#libraries:
#- name: jinja2
#  version: latest
Run Code Online (Sandbox Code Playgroud)

这是整个应用程序的基本html模板:

模板/应用 - 视图 - wrapper.html

<!-- templates/app-view-wrapper.html -->
<!DOCTYPE HTML>

<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html class="no-js" lang="en" ng-app="rcsApp">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" src="//normalize-css.googlecode.com/svn/trunk/normalize.css" />
        <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.css"/>
        <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/5.5.1/css/foundation.min.css"/>
        <link rel="stylesheet" href="/css/style.css">
        <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
        <script src="https://code.angularjs.org/1.3.15/angular.min.js"></script>
        <script src="https://code.angularjs.org/1.3.15/angular-route.min.js"></script>
        <script src="/js/app.js"></script>
        <title>Website Title</title>
    </head>
    <body>
        <header>
            <div class="row">
                <div class="large-4 columns"><img src="/img/website-logo.png" alt="Website logo"></div>
                <div class="large-8 columns">
                        <a href="#" class="button right">555-555-5555</a>
                        <a href="#" class=" button right">Make an Appointment</a>
                </div>
            </div>
            <div class="row" id="nav-row">
                <nav class=" top-bar">
                    <section class=" top-bar-section">
                        <ul class="left">
                            <li><a href="/">Home</a></li>
                            <li><a href="/">Services</a></li>
                            <li><a href="/">Meet the Doctor</a></li>
                            <li><a href="/">Patients</a></li>
                            <li><a href="/referrals">Referring Doctors</a></li>
                            <li><a href="/">Contact Us</a></li>
                        </ul>
                    </section>
                </nav>
            </div>
            <div ng-view></div>
        </header>
        <footer class="row">
            <div class="large-5 columns">
                <h3>Location</h3>
                <div>123 ABC STREET</div>
                <div>Phone Number:  555-5555555</div>
                <div>Email: email@email.com</div>

            </div>
            <div class="large-4  columns">
                <h3>Quick Contact</h3>

                <div>Work:  555-5555555</div>
                <div>Cell:   555-5555555</div>
                <div>Fax: 555-5555555</div>
            </div>
            <div class="large-3 columns">
               Lorem Ipsum Sit Dolor Amet
            </div>

        </footer>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
        <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.min.js"></script>
        <script src="/js/script.js"></script>
        <script>
            $(document).foundation();
        </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以下是三个模板:

模板/欢迎-page.html中

<!-- templates/welcome-page.html -->
<div><h1>MAIN PAGE</h1></div>
Run Code Online (Sandbox Code Playgroud)

模板/ referrals.html

<!-- templates/referrals.html -->
<div><h1>REFERRALS PAGE</h1></div>
Run Code Online (Sandbox Code Playgroud)

模板/ 404.html

<!-- templates/404.html -->
<div><h1>404</h1></div>
Run Code Online (Sandbox Code Playgroud)

文件层次结构如下:

- rcsbackend
  - templates
  - static
    - img
    - js
    - css
  - app.yaml
  - main.py
Run Code Online (Sandbox Code Playgroud)

Pan*_*kar 4

您的解决方案之所以有效,是因为默认情况下被禁用,这就是为什么角度路由只能识别之后html5mode的原因,并且您在 URL 为您工作之前放置了路线。URL#

解决方案

您需要html5mode在应用程序中启用才能使路由正常工作,只需$locationProvider.html5Mode(true)在 Angular 的配置阶段进行即可。

代码

.config(function($locationProvider) {
    $locationProvider.html5Mode(true);
})
Run Code Online (Sandbox Code Playgroud)

现在你可以回到你的旧代码,将解决你的问题。

更新

要使用相对链接来链接您的应用程序,您需要在文档中设置 。

<base href="/">
Run Code Online (Sandbox Code Playgroud)

请参阅此答案将在应用程序中启用时为您提供帮助html5mode