0

So I have a json file with nested elements. This is my json file:

    [
{
    "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 this is my controller that i used to access the json :

   var app= angular.module('myApp', []);

   app.controller('ListCtrl', ['$scope', '$http', 
  function($scope, $http) {
    $http.get('results.json').success(function(data) {
        $scope.questions = data; // get data from json
          angular.forEach($scope.questions, function(item){
               console.log(item.images);  
           })
        });


    });

  }
]); 

And then my html code to display the questions and each questions list of images using the ng-repeat :

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

As of now iam trying to display the questions id from the json file as a list however the output im getting is a:

  • {{question.qid}}

as the output in my html page.. Can someone please help me. I dont know what im doing wrong.

Shayuh
  • 361
  • 1
  • 3
  • 11

4 Answers4

0

You haven's define the app so you should change the first line of your code to this and check your closing tags on your js code...:

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; // get data from json

        angular.forEach($scope.questions, function(item){
            console.log(item.images);  
        })

    });
}

]);
Vassilis Pits
  • 3,788
  • 4
  • 33
  • 48
  • Still doesnt seem to be working.. the question ids from the json doesnt show up in the html – Shayuh Oct 26 '16 at 09:09
  • Yes!! I get a: Uncaught ReferenceError: angular is not defined at http://localhost:8012/questionnaire.php:112:11 ... this is where my :
  • {{question.qid}}
  • line is. – Shayuh Oct 26 '16 at 09:17
  • Did import angular to your project through a cdn or somewhere? Please update your question with your full HTML code. – Vassilis Pits Oct 26 '16 at 09:23