Angular的ng-repeat与预定义的标记

时间:2022-08-23 23:32:29

How to import ready html list of objects to angulars store?

如何将现成的html对象列表导入到angulars商店?

For example in Django template I could do something like:

例如,在Django模板中,我可以执行以下操作:

{% for product in products %}
    <div class="product">{{ product.title }}</div>
{% endfor %}
<div class="product" ng-repead="product in products">{% verbatim %}{{ product.title }}{% endverbatim %}</div>

Django's loop used to output products to user and Angular's loop used for creating new products with javascript.

Django的循环用于向用户输出产品,Angular的循环用于使用javascript创建新产品。

What is correct way to load data to angular?

将数据加载到角度的正确方法是什么?

1 个解决方案

#1


1  

You can just generate a json representation of your data and assign it to a $scope variable in your controller.

您只需生成数据的json表示形式,并将其分配给控制器中的$ scope变量。

Normally, you would fetch data from server using a REST API, through $http or $resource.

通常,您可以使用REST API通过$ http或$ resource从服务器获取数据。

But if you want to avoid the extra calls, you can include the data in the first html, inside a <script> tag. Output the data in JSON and assign it to a variable. Then use it wherever you need it.

但是如果你想避免额外的调用,你可以将数据包含在

Like this:

喜欢这个:

<html>
<body>

    <div ng-repeat="item in items">{{item}}</div>

    <script>
        var data = { ... your data here, generated by Django when serving the HTML ... }

        ... in your controller...
        $scope.items = data;
        ...
    </script>
</body>
</html>

#1


1  

You can just generate a json representation of your data and assign it to a $scope variable in your controller.

您只需生成数据的json表示形式,并将其分配给控制器中的$ scope变量。

Normally, you would fetch data from server using a REST API, through $http or $resource.

通常,您可以使用REST API通过$ http或$ resource从服务器获取数据。

But if you want to avoid the extra calls, you can include the data in the first html, inside a <script> tag. Output the data in JSON and assign it to a variable. Then use it wherever you need it.

但是如果你想避免额外的调用,你可以将数据包含在

Like this:

喜欢这个:

<html>
<body>

    <div ng-repeat="item in items">{{item}}</div>

    <script>
        var data = { ... your data here, generated by Django when serving the HTML ... }

        ... in your controller...
        $scope.items = data;
        ...
    </script>
</body>
</html>