【问题标题】:angularjs unable to find templates directory, flask as backendangularjs 找不到模板目录,flask 作为后端
【发布时间】:2013-12-24 07:10:34
【问题描述】:

我在客户端使用flask作为后端和angularjs

我的目录结构:

露水:

  ->app.py

  ->templates
    ->hello.html
    ->test.html

  ->static
    ->js
      ->directives.py
    ->lib
      ->angular.js

我的 app.py 文件:

from flask import Flask, make_response,render_template
@app.route("/aboutUs")
def aboutUs():
    return render_template("test.html", title="test page")

我的directions.py 文件:

angular.module('components',[])
    .directive("helloWorld",function($scope,$log){
        $scope.$log = $log;
        return{
            restrict:"A",
            templateUrl:"templates/hello.html"
        }
    })

angular.module('testApp',['components'])

Flask 能够正确渲染 test.html 模板,但 angular 显示 hello.html,找不到模板错误

【问题讨论】:

    标签: angularjs flask angularjs-directive


    【解决方案1】:

    1。神社模板

    如果hello.html 包含 Jinja2 标记,则它是一个模板,需要由 Flask 呈现。添加@app.route 来渲染它。

    @app.route("/hello")
    def hello():
        return render_template("hello.html")
    

    然后指向 Angular 指令中的hello URL。

    templateUrl: "{{ url_for('hello'}} }}"
    

    2。角模板

    如果文件只包含 HTML(没有 Jinja2 标记),那么它不是模板。它不应该在templates 文件夹中,而是在静态文件夹中;喜欢static/angular_templates/hello.html

    然后在指令中指向静态文件。

    templateUrl: "{{ url_for('static', filename='angular_templates/hello.html') }}"
    

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 2012-01-18
      • 2012-07-18
      • 2015-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多