从烧瓶传递一个字符串到angularjs [英] Passing a string from flask to angularjs
问题描述
我将一个变量从烧瓶传递到这样的模板
I pass a variable from flask to a template like this
@app.route('/')
def respond():
return render_template('index.html', bar='foo')
在附加到该页面正文的angularjs控制器中,我有一个这样的函数:
In angularjs controller attached to the body of that page I have a function like this:
$scope.init = function(bar){
$scope.content = bar;
}
我试图将此变量从flask传递到模板中的角度,就像这样:
I am trying to pass this variable from flask to angular in the template like this:
<body ng-init="init({{ bar|tojson|safe }})" ng-controller="AppCtrl">
但最终DOM中的标签搞砸了
But in the end the tag in DOM is messed up
<body ng-init="init("foo")" ng-controller="AppCtrl">
这个问题是引号。浏览器无法正确解释它们并且不传递变量。
The problem with this are quotation marks. Browser cannot interpret them properly and the variable isn't passed.
所以我的问题是如何解决这个问题?我不想再从角度控制器发出另一个http请求来获取该数据。
So my question is how can I solve this? I don't want to make another http request from angular controller just to get that data.
编辑。
<body ng-init="init('{{ bar }}')" ng-controller="AppCtrl">
好像已经修好了
推荐答案
只需使用'
,而DOM属性已经开始使用
Simply use '
while DOM attribute already has started with "
Double double qoute将打破DOM。
Double inside double qoute will break the DOM.
<body ng-init="init('{{ bar|tojson|safe }}')" ng-controller="AppCtrl">
将在DOM上呈现为
<body ng-init="init('foo')" ng-controller="AppCtrl">
希望这可以帮到你。谢谢。
Hope this could help you. Thanks.
这篇关于从烧瓶传递一个字符串到angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!