1

I have a Json page like this :

    [{
       "qid": "1",
       "contester": "0",
       "answer": "0",
       "question": "What are you most likely to do after getting into an argument?",
       "images": [{
               "qid": "1",
               "imageid": "AB100",
               "imgname": "doghug_q1",
               "imgpath": "Images\/Q1\/doghug_q1.jpg"
            }, 
            {
               "qid": "1",
               "imageid": "AB101",
               "imgname": "eq_q1.jpg",
               "imgpath": "Images\/Q1\/eat_q1.jpg"
            }, {
               "qid": "1",
               "imageid": "AB102",
               "imgname": "headache_q1",
               "imgpath": "Images\/Q1\/headache_q1.jpg"
            }, {
              "qid": "1",
              "imageid": "AB106",
              "imgname": "scream_q1.jpg",
              "imgpath": "Images\/Q1\/scream_q1.jpg"
            }, {
              "qid": "1",
              "imageid": "AB107",
              "imgname": "shopping_q1",
              "imgpath": "Images\/Q1\/shopping_q1.jpg"
            }, {
              "qid": "1",
              "imageid": "AB108",
              "imgname": "walkAlone_q1",
              "imgpath": "Images\/Q1\/walkAlone_q1.jpg"
            }]
     }, {
          "qid": "2",
          "contester": "0",
          "answer": "0",
          "question": "Which game would you rather play?",
          "images": [{
                   "qid": "2",
                   "imageid": "AB105",
                   "imgname": "charades_q2.jpg",
                   "imgpath": "Images\/Q2\/charades_q2.jpg"
              }, {
                   "qid": "2",
                   "imageid": "AB109",
                   "imgname": "playingCards_q2.jpg",
                   "imgpath": "Images\/Q2\/playingCards_q2.jpg"
              }, {
                   "qid": "2",
                   "imageid": "AB110",
                   "imgname": "chess_q2",
                   "imgpath": "Images\/Q2\/chess_q2.jpg"
              }, {
                   "qid": "2",
                   "imageid": "AB111",
                   "imgname": "twister_q2",
                   "imgpath": "Images\/Q2\/twister_q2.jpg"
             }]
   }]

and my controller code is this :

    var app = angular.module('myApp', []);
    app.controller('ListCtrl', ['$scope', '$http', function($scope, $http) {
        $http.get('results.json').success(function(data) {
  
            $scope.questions = []; 

            angular.forEach(data, function(question) {
                  $scope.questions.push(question)

             });

           $scope.images=[];// get data from json
           angular.forEach($scope.questions, function(sorg) {
                   angular.forEach(sorg.images, function(image) {
                        $scope.images.push(image)

                    });
           });

          console.log($scope.images);
       });
   }]);

And in order to display the relevant question and its answer images the html is as follows :

    <body ng-app="myApp" >
        <div ng-controller="ListCtrl">
              <ul>
        
                  <li ng-repeat="question in questions"> {{question.qid}}. {{question.question}} </li>
                  <li ng-repeat="image in images"> {{image.imgpath}}  </li>
        
             </ul>
        </div>

However the output shows this :

  1. What are you most likely to do after getting into an argument?

  2. Which game would you rather play?

    Images/Q1/doghug_q1.jpg Images/Q1/eat_q1.jpg Images/Q1/headache_q1.jpg Images/Q1/scream_q1.jpg Images/Q1/shopping_q1.jpg Images/Q1/walkAlone_q1.jpg Images/Q2/charades_q2.jpg Images/Q2/playingCards_q2.jpg Images/Q2/chess_q2.jpg Images/Q2/twister_q2.jpg

but i want the output to be like this :

  1. What are you most likely to do after getting into an argument?

    Images/Q1/doghug_q1.jpg Images/Q1/eat_q1.jpg Images/Q1/headache_q1.jpg Images/Q1/scream_q1.jpg Images/Q1/shopping_q1.jpg Images/Q1/walkAlone_q1.jpg

  2. Which game would you rather play?

    Images/Q2/charades_q2.jpg Images/Q2/playingCards_q2.jpg Images/Q2/chess_q2.jpg Images/Q2/twister_q2.jpg

So basically i want the images for question two to show up below question 2 and the ones for question 1 below question one. However the code doesnt seem to be working the way i want it to because it shows all the questions first and then all the image answers in the end. Is their any way of fixing this? Am I using ng-repeat in the wrong way? please help ..

3 Answers 3

1

You don't need the array images for this, because images are inside questions, then you can to do:

angular.forEach(data, function(question) {
      $scope.questions.push(question);
});

HTML:

 <ul>
    <li ng-repeat="question in questions"> {{question.qid}}. {{question.question}} 
        <div ng-repeat="image in question.images"> {{image.imgpath}}  </div>
    </li>
 </ul>

And remove this block:

$scope.images=[];// get data from json
angular.forEach($scope.questions, function(sorg) {
     angular.forEach(sorg.images, function(image) {
          $scope.images.push(image)
     });
 });

Plunker

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

3 Comments

Yes i did try that however, then only the questions appear. The next <li> </li> for the images does not show image paths at all ..
This will show only the questions. No images. Here's the [plunker] (plnkr.co/edit/UQk5i8LJlE3jSC0ISJSP?p=preview)
Thankyou!! finally worked ^.^ link
0

JS

var app = angular.module('myApp', []);
app.controller('ListCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.questions = []
    $http.get('results.json').success(function(data) {
        $scope.questions = data;
   });
}]);

HTML

<div ng-controller="ListCtrl">
          <ul>
              <li ng-repeat="question in questions"> {{question.qid}}. {{question.question}}>
                   <p ng-repeat="image in question.images"> {{image.imgpath}}  </p>
              </li>
         </ul>
    </div>

2 Comments

I tried this doesnt seem to be working. Here's the plunker for your suggestion. link
@Y.Hewa i found an error, by the way, you request results.json, but you have result.json
0
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">


<div ng-repeat="x in records">
  <b>{{x.question}}</b><br><br>

  <div ng-repeat="y in x.images">{{y.imgpath}}<br><br></div>
</div>


<script>
var app = angular.module("myApp", []);
app.controller("myCtrl",['$scope','$http', function($scope,$http) {

    $http.get("result.json")
  .then(function(response) {
      $scope.records = response.data;
  }, function(response) {
      $scope.records = "Something went wrong";
  });

}]);
</script>

</body>
</html>

4 Comments

Please explain the solution you offer and how to use it.
Just copy and paste the full code in plain HTML file and run it.
This works if the json is embedded in the script. However, when i try to change it to read external .json file it does not work..
It works fine even if we work with externel json file

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.