簡體   English   中英

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

[英]How to assign value in $scope inside ng-repeat angularJS

我現在對angularJS中的ng-repeat有一個問題,如何在ng-repeat的$ scope中分配值。

這是我的場景:

我有一個滿足所有數據的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"}
]

現在這是我的示例數據。

單擊書本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

請幫助我如何從chap$scope.currentChapter賦值

非常感謝..

您可以創建自定義裝配器,並將“章”作為參數傳遞。 或者你可以寫:

    <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表示“完全匹配”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM