django 代码在 angular ng-repeat 中只执行一次
django code is execude only once in an angular ng-repeat
全部,
我在 Angular“ng-repeat”块中有一些 HTML,其中包括调用 Django 模板标记。但是,该标记只会被调用一次。这是一个人为的例子:
my_template.html
<div ng-repeat="item in items" ng-init="items=['a','b','c']">
{% verbatim %}
the item is: {{a}}
{% endverbatim %}
the template tag returns: {% my_template_tag %}
</div>
my_template_tags.py
my_generator = (i for i in ['a','b','c'])
@register.simple_tag
def my_template_tag():
return my_generator.next()
这正确地呈现了 3 个 div,但它们的内容有误:
<div...>
the item is: a
the template tag returns: a
</div>
<div...>
the item is: b
the template tag returns: a
</div>
<div...>
the item is: c
the template tag returns: a
</div>
该模板标签只被调用一次。有人知道为什么吗?
谢谢。
Django 模板在 Angular 执行任何操作之前呈现 HTML。 Django 模板不知道您将在前端进行迭代。所以,当 Django 完成处理后,代码如下所示:
<div ng-repeat="item in items" ng-init="items=['a','b','c']">
the item is: {{a}}
the template tag returns: a
</div>
如果您使用 Django 模板进行迭代,则需要在服务器端实际呈现要创建的所有 div。 Django 迭代并没有真正与前端的 ng-repeat
混合。
全部,
我在 Angular“ng-repeat”块中有一些 HTML,其中包括调用 Django 模板标记。但是,该标记只会被调用一次。这是一个人为的例子:
my_template.html
<div ng-repeat="item in items" ng-init="items=['a','b','c']">
{% verbatim %}
the item is: {{a}}
{% endverbatim %}
the template tag returns: {% my_template_tag %}
</div>
my_template_tags.py
my_generator = (i for i in ['a','b','c'])
@register.simple_tag
def my_template_tag():
return my_generator.next()
这正确地呈现了 3 个 div,但它们的内容有误:
<div...>
the item is: a
the template tag returns: a
</div>
<div...>
the item is: b
the template tag returns: a
</div>
<div...>
the item is: c
the template tag returns: a
</div>
该模板标签只被调用一次。有人知道为什么吗?
谢谢。
Django 模板在 Angular 执行任何操作之前呈现 HTML。 Django 模板不知道您将在前端进行迭代。所以,当 Django 完成处理后,代码如下所示:
<div ng-repeat="item in items" ng-init="items=['a','b','c']">
the item is: {{a}}
the template tag returns: a
</div>
如果您使用 Django 模板进行迭代,则需要在服务器端实际呈现要创建的所有 div。 Django 迭代并没有真正与前端的 ng-repeat
混合。