0

This is my code where I get the data from a json file an put it in an array. I want to take from my array only 5 items, but I want to do that code in the app.js file, not in html file. So I don't want to use ng-repeat with limitTo.

$http.get('auto_quiz_data.json').then(function(quizData){
        $scope.myQuestions = quizData.data;
        $scope.totalQuestions = $scope.myQuestions.length;
        shuffleArray($scope.myQuestions);

    });

and this is the html

    <div id="myQuiz" ng-controller="QuizController">
<p class="txt">{{totalQuestions}}</p>
</div>

Someone please tell me how to print in the view only 5 items.

dragon
  • 1,212
  • 13
  • 24
  • 1
    Are you using ng-repeat to show the questions? – Satej S Mar 01 '16 at 16:27
  • yes, but I don't want to use the filter limitTo. I want the code to be in the js file, that brings only 5 items, not in the html file. – dragon Mar 01 '16 at 16:37
  • 1
    @dragon You can also use [`slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) to copy a subsection of an array. – Mike Cluck Mar 01 '16 at 16:48
  • I am new to angular Mike. Please can you show me my code modified with slice? – dragon Mar 01 '16 at 16:52
  • It's not Angular, it's just JS. Look at the examples on the page I linked to. – Mike Cluck Mar 01 '16 at 17:21
  • I used this in my code `$scope.myQuestions = quizData.data.slice(0, 5);` and I get the error `TypeError: Cannot read property 'slice' of undefined` – dragon Mar 01 '16 at 17:25
  • Very strange only on google chrome I get that error. In other browsers work fine. – dragon Mar 01 '16 at 17:31

1 Answers1

0

I gest that this is what you are look for,

<div ng-repeat="item in myQuestions | limitTo: 5">
    <p>{{item}}</p>
</div>

in angular you can use the limitTo filter to customize the number of items to show Hope this help?

tony pro
  • 405
  • 3
  • 12
  • my friend I don't want to use the filter limitTo. I want the code to be in the js file, that brings only 5 items, not in the html file. – dragon Mar 01 '16 at 16:42