我正在尝试将 ng-repeat
用于列表。但我想要使用 Django 呈现的预先存在的列表项。
注意:我已将 {[{ }]}
设置为我的 AngularJS InterpolateProvider。
HTML 示例
<ul ng-controller="ListController">
{% for item in existing_list %}
<li>
<span>{{item.firstName}}</span>
<span>{{item.lastName}}</span>
</li>
{% endfor %}
<li ng-repeat="item in items">
<span>{[{item.firstName}]}</span>
<span>{[{item.lastName}]}</span>
</li>
</ul>
现在,我想使用 ng-controller
处理这些项目
app.js
function ListController($scope){
$scope.items = [{firstName: "Bob", lastName: "Smith"}];
}
我的问题是,如何将 Django 生成的预先存在的列表项添加到 $scope.items
中?
最佳答案
我现在的做法是使用 ngInit
将现有数据注入(inject)范围。所以你会有:
<ul ng-controller="ListController" ng-init='items = {% existing_list.toJSON() %}'>
(抱歉,我不知道如何将对象序列化为 JSON)
关于javascript - AngularJS - 如何处理 ng-repeat 与预先存在的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15869632/