0

I want to display some reviews that are related to subjects inside a data array, $scope.allSubjects. However... the data array will be filtered out in the client to only show like 10 things, out of 1000a+ things. In my api, each subject has a lot of reviews. How can I only get the reviews associated with the data array's currently filtered items? Each subject has an id, and I plan to use that to get reviews that are related to that subjects id through getInitialReviews and attach them to $scope.allSubjects. Then, I would use a nested ngRepeat to show all the reviews within that subject. My first approach was to just use a for loop to itereate through the entire $scope.allSubjects to get back the id so I could get review data from my API, but I feel that would be too much load for the client if there are 1000+ subjects. So... is this even a good approach for what I'm trying to do?

js lint pseudo code: https://jsfiddle.net/zw6zpxou/

pyramidface
  • 1,207
  • 2
  • 17
  • 39

1 Answers1

0

I think you are assuming to display only filtered subjects on view. if it is the case than, you'd be better to use sub controller and pass only filtered data into it by using ng-init.

https://jsfiddle.net/mrno1dfr/1/

<div ng-repeat="x in allSubjects | your_filter">

    <div class="col-md-2 subject-card">
        {{ x.name }}
        <!-- reviews go here with their own loop?  -->
        <div ng-controller="reviewsCtrl" ng-init="loadReviews(x.subject_id)">
            reviews count : {{len(reviews)}}
            <div ng-repeat="review in reviews">
                {{review.someData}}
            </div>
        </div>
    </div>

angular.module('mainApp')
.controller('reviewsCtrl', ['$scope', function($scope) {
    $scope.loadReviews = function(subject_id) {
        gridFactory.getReviews(subject_id)
        .then(function(result) {
           scope.reviews = result.data;
        });
    }
});

I'm not sure about the exact result data structure though..

HyoukJoon Lee
  • 151
  • 1
  • 6
  • Hmm I'll use this as a backup strategy. I should've updated this question.. I found what I was looking for through: http://stackoverflow.com/questions/11721863/angularjs-how-to-get-an-ngrepeat-filtered-result-reference – pyramidface Jul 29 '15 at 06:49