【问题标题】:AngularJS - How to deal with ng-repeat with pre-exist itemsAngularJS - 如何处理 ng-repeat 与预先存在的项目
【发布时间】:2013-03-29 23:25:31
【问题描述】:

我正在尝试将 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 中?

【问题讨论】:

  • 似乎将数据合并为 JSON 并使用 ng-repeat 创建所有项目更有意义。服务器上列表的硬编码部分的缺点是失去使用角度过滤列表的能力
  • 只是想知道您是否解决了这个问题以及如何解决。我目前面临同样的问题。

标签: javascript django angularjs


【解决方案1】:

我现在的做法是使用ngInit 将现有数据注入范围。所以你会有这样的:

<ul ng-controller="ListController" ng-init='items = {% existing_list.toJSON() %}'>

(对不起,我不知道如何将对象序列化为 JSON)

【讨论】:

  • 感谢您的回答。我已经想到了,但我真的想避免这种打印 JSON 以供 js 使用的确切方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
相关资源
最近更新 更多