我想使用AngularJS与Django,但他们都使用{{}}作为他们的模板标签。是否有一种简单的方法来更改其中之一,以使用其他自定义模板标签?
当前回答
所以今天我在Angular IRC频道得到了很大的帮助。事实证明,你可以很容易地更改Angular的模板标签。以下必要的代码片段应该包含在你的angular include之后(给定的例子会出现在他们的邮件列表中,并且会使用(())作为新的模板标签,代替你自己的):
angular.markup('(())', function(text, textNode, parentElement){
if (parentElement[0].nodeName.toLowerCase() == 'script') return;
text = text.replace(/\(\(/g,'{{').replace(/\)\)/g, '}}');
textNode.text(text);
return angular.markup('{{}}').call(this, text, textNode, parentElement);
});
angular.attrMarkup('(())', function(value, name, element){
value = value.replace(/\(\(/g,'{{').replace(/\)\)/, '}}');
element[0].setAttribute(name, value);
return angular.attrMarkup('{{}}').call(this, value, name, element);
});
此外,我还指出了即将进行的增强,它将公开startSymbol和endSymbol属性,可以将它们设置为您想要的任何标记。
其他回答
你可以试试逐字逐句的Django模板标签 像这样使用它:
< script src = " https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js " > < /脚本> {% verbatim %} < div ng-app = " " > <p>10是{{5 + 5}}</p> < / div > {% endverbatim %}
如果你使用django 1.5及更新版本:
{% verbatim %}
{{if dying}}Still alive.{{/if}}
{% endverbatim %}
如果你还在使用appengine上的django 1.2,可以使用下面的命令来扩展django语法…
from django import template
register = template.Library()
class VerbatimNode(template.Node):
def __init__(self, text):
self.text = text
def render(self, context):
return self.text
@register.tag
def verbatim(parser, token):
text = []
while 1:
token = parser.tokens.pop(0)
if token.contents == 'endverbatim':
break
if token.token_type == template.TOKEN_VAR:
text.append('{{')
elif token.token_type == template.TOKEN_BLOCK:
text.append('{%')
text.append(token.contents)
if token.token_type == template.TOKEN_VAR:
text.append('}}')
elif token.token_type == template.TOKEN_BLOCK:
text.append('%}')
return VerbatimNode(''.join(text))
在你的文件中使用:
from google.appengine.ext.webapp import template
template.register_template_library('utilities.verbatim_template_tag')
来源: http://bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html
我发现下面的代码很有用。我在这里找到了代码:http://djangosnippets.org/snippets/2787/
"""
filename: angularjs.py
Usage:
{% ng Some.angular.scope.content %}
e.g.
{% load angularjs %}
<div ng-init="yourName = 'foobar'">
<p>{% ng yourName %}</p>
</div>
"""
from django import template
register = template.Library()
class AngularJS(template.Node):
def __init__(self, bits):
self.ng = bits
def render(self, ctx):
return "{{%s}}" % " ".join(self.ng[1:])
def do_angular(parser, token):
bits = token.split_contents()
return AngularJS(bits)
register.tag('ng', do_angular)
如果你正确地分离了页面的各个部分,那么你可以很容易地在“raw”标签范围内使用angularjs标签。
在jinja2
{% raw %}
// here you can write angularjs template tags.
{% endraw %}
在Django模板中(1.5以上)
{% verbatim %}
// here you can write angularjs template tags.
{% endverbatim %}
所以今天我在Angular IRC频道得到了很大的帮助。事实证明,你可以很容易地更改Angular的模板标签。以下必要的代码片段应该包含在你的angular include之后(给定的例子会出现在他们的邮件列表中,并且会使用(())作为新的模板标签,代替你自己的):
angular.markup('(())', function(text, textNode, parentElement){
if (parentElement[0].nodeName.toLowerCase() == 'script') return;
text = text.replace(/\(\(/g,'{{').replace(/\)\)/g, '}}');
textNode.text(text);
return angular.markup('{{}}').call(this, text, textNode, parentElement);
});
angular.attrMarkup('(())', function(value, name, element){
value = value.replace(/\(\(/g,'{{').replace(/\)\)/, '}}');
element[0].setAttribute(name, value);
return angular.attrMarkup('{{}}').call(this, value, name, element);
});
此外,我还指出了即将进行的增强,它将公开startSymbol和endSymbol属性,可以将它们设置为您想要的任何标记。
推荐文章
- Babel 6改变了它导出默认值的方式
- 如何配置历史记录?
- ES6模板文字可以在运行时被替换(或重用)吗?
- [Vue警告]:找不到元素
- 如何在django应用程序中显示favicon ?
- 可以在setInterval()内部调用clearInterval()吗?
- AngularJS控制器的生命周期是什么?
- 无法读取未定义的属性“msie”- jQuery工具
- 我的蛋蛋怎么不见了?
- JavaScript中的排列?
- JavaScript中有睡眠/暂停/等待功能吗?
- 如何禁用文本选择使用jQuery?
- 如何停止事件冒泡复选框点击
- 向对象数组添加属性
- 如何在Redux应用程序中动态加载代码分割的减速器?