0

I have a problem now regarding the ng-repeat in angularJS on how to assign value in $scope inside ng-repeat..

This is my scenorio:

I have a json file that content all the data. It shows like this..

[
{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"}
]

Now that is my Example data..

When Book 1 is click, I want to Display it like this..

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

This is my Code:

//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>

In my Controller :

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

Now the Problem is that where i can get the $scope.currentChapter which must the value of chap

Please help me how assign value to $scope.currentChapter from the chap

Thank you so much..

5
  • You are approaching this from the wrong angle. Transform your data into something sensible/hierarchical, then the view binding will be simple. Commented Oct 8, 2014 at 10:39
  • yes i also know about that but the data came from other people..and its a lot to work for so i need to find a solution to use that.. Commented Oct 8, 2014 at 11:04
  • i know it is simple if i array the content inside the chapter and array the chapter inside a book.. Commented Oct 8, 2014 at 11:05
  • It looks like a pretty data transformation to me. Using an underscore mixin it's just _(books).groupByMultiple(['BookID', 'Chapter', 'ContentID']) Commented Oct 8, 2014 at 11:11
  • oh pretty cool huh..that's nice..thanks for that new information for me..thanks.. Commented Oct 9, 2014 at 1:45

1 Answer 1

1

You can create custom fiter and pass 'chap' as a parameter. Or u can write:

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

Two filters here for bookId and chapter, :true means 'exact match'.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks pal..this is awesome... almost a day i've searching for this but now i got it..this is absolute what i need..
And thanks for the info about :true now i know..thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.