如何在ng-repeat angularJS中的$ scope中赋值

时间:2014-10-08 09:54:43

标签: javascript html angularjs

我现在有一个关于如何在ng-repeat内的$ scope中赋值的angularJS中的ng-repeat的问题。

这是我的场景:

我有一个内容所有数据的json文件。它显示像这样......

[
{BookID:1,Chapter:1,ContentID:1,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:1,ContentID:2,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:1,ContentID:3,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:1,ContentID:4,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:2,ContentID:1,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:2,ContentID:2,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:2,ContentID:3,Content:"The quick brown fox is nice"},
{BookID:1,Chapter:2,ContentID:4,Content:"The quick brown fox is nice"},
{BookID:2,Chapter:1,ContentID:1,Content:"The quick brown fox is nice"},
{BookID:2,Chapter:1,ContentID:2,Content:"The quick brown fox is nice"},
{BookID:2,Chapter:1,ContentID:3,Content:"The quick brown fox is nice"},
{BookID:2,Chapter:1,ContentID:4,Content:"The quick brown fox is nice"}
]

现在这是我的示例数据..

单击Book 1时,我想像这样显示..

Book 1

Chapter 1
1 The quick brown fox is nice. 2 The quick brown fox is nice. 3 The quick brown fox is nice. 4 The quick brown fox is nice

Chapter 2
1 The quick brown fox is nice. 2 The quick brown fox is nice. 3 The quick brown fox is nice. 4 The quick brown fox is nice

这是我的代码:

//Chapter is expected in every book so i just loop it depending on expected chapters (e.i, 1,2,3,4

<div data-ng-repeat="chap in Chapters">
        <span style="font-size:20px">{{chap}}</span>
        <span data-ng-repeat="testament in Testaments | filter:filters">
            <b>{{testament.ContentID}}</b> {{testament.Content}}
        </span>
        <br />
    </div>

在我的控制器中:

$scope.filters = function (row) {
        var bookId = row.BookID.toString();
        var chapter = row.Chapter.toString();
        return ( bookId == $scope.bookId && chapter == $scope.currentChapter);
    };

现在的问题是,我可以获得$scope.currentChapter,其值必须为chap

请帮助我如何从$scope.currentChapter

chap分配价值

非常感谢你..

1 个答案:

答案 0 :(得分:1)

您可以创建自定义适配器并通过“快速解决方案”。作为参数。或者你可以写:

    <span data-ng-repeat="testament in Testaments | filter:{'BookID':bookId}:true | filter:{Chapter:chap}:true">
        <b>{{testament.ContentID}}</b> {{testament.Content}}
    </span>

这里有两个过滤器,用于bookId和chapter,:true表示&#39;完全匹配&#39;。