How to get Angular-Flask app to load html partials

2019-05-10 01:26发布

I am trying to get my Angular-Flask App to render partial HTML files in a base html file. The application loads the base html (Window Title and Footer load) but nothing loads for the ng-view. Perhaps my angular routing to the partials is not correct?

File Structure

->flaskapp
    ->static
        ->js
            ->appController.js
            ->routing.js
            ->homeController.js
            ...
    ->views
        ->home
            ->__init__.py
            ...
    ->templates
        ->partials
            ->home.html
            ...
        ->base.html

home

mod = Blueprint('home', __name__)

@mod.route('/')
def index():
    return render_template('main.html')

base.html

<!DOCTYPE html>
<html ng-app="FlaskApp">
<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular-resource.min.js"></script>
    <script src=/static/js/appController.js></script>
    <script src=/static/js/homeController.js></script>
    <script src=/static/js/bootstrap.js></script>

    {% block head %}
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %} - Flask POC</title>
    {% endblock %}
</head>

<body>
    <div ng-controller="AppController">
        <div ng-view>
    </div>
    <br />
    <div id="footer">
        {% block footer %}
        &copy; Copyright 2014
        {% endblock %}
    </div>
</body>
</html>

home.html

<p>Just some Random Text</p>

appController.js

'use strict';
var app = angular.module('FlaskApp', ['ngRoute']);

app.controller('AppController', function($scope){

});

homeController.js

'use strict';

var app = angular.module('FlaskApp');

app.controller('HomeController', function(){

});

routing.js

'use strict';

var app = angular.module('FlaskApp');

app.config(function($routeProvider){
    $routeProvider.when('/', {
        templateURL: 'partials/home.html',
        controller: 'HomeController'    
    }).otherwise({ redirectTo: '/' });
});

Does anyone know why I cant get partials to load? Thanks a lot.

Attempts

  1. Changed templateURL to:
    • /partials/home.html
    • ../partials/home.html
    • static/partials/home.html
    • /static/partials/home.html
    • ../static/partials/home.html

If it helps, here is the link of the tutorial I have been following: http://blog.pangyanhan.com/posts/2013-08-27-ngtut.html

  1. Moved base.html into the static folder

2条回答
Rolldiameter
2楼-- · 2019-05-10 01:40

Try naming your partials with .tpl.html.

Would changing templateURL to:

templateURL: 'home.tpl.html',

work at all for you?

查看更多
孤傲高冷的网名
3楼-- · 2019-05-10 01:43

Flask doesn't serve files from the templates folder. If you wish to keep home.html in there, you will need to define a route that matches the URL and serves the file. You can use this as a starting point.

@app.route('/partial/<path:path>')
def serve_partial(path):
    return render_template('/partial/{}'.format(path))

If you don't want to have to create such a view, you can also move the partial templates into static. If your directory structure looked like

static
|---- partial
|     |---- home.html

you'd be able to access the template with

app.config(function($routeProvider){
    $routeProvider.when('/', {
        templateURL: '/static/partials/home.html',
        controller: 'HomeController'    
    }).otherwise({ redirectTo: '/' });
});
查看更多
登录 后发表回答