Shashank Vivek
Shashank Vivek

Reputation: 17504

JSON Data not getting loaded in Angular JS

I am new to AngularJS & JSON and giving a try on below example, but I am unable to load the data from JSON file:

<body>
    <h2>AngularJS Sample Application</h2>
    <div ng-app="" ng-controller="kittyController">
        <table>
            <tr>
                <th>Name</th>
                <th>Roll No</th>
                <th>Percentage</th>
            </tr>
            <tr ng-repeat="kitty in kitties">
                <td>{{ kitty.Name}}</td>
                <td>{{ kitty.RollNo}}</td>
                <td>{{ kitty.Percentage.num}} -- {{kitty.Percentage.avrg}}</td>
            </tr>
        </table>
    </div>
    <script>
        function kittyController($scope, $http) {
            var url = "data.json";
            $http.get(url).success(function(response) {
                $scope.kitties = response;
            });
        }
    </script>

    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">    </script>
  </body>

data.json

[
{
"Name" : "kitty 1",
"RollNo": 101,
"Percentage" : [
{"num" : 88,
  "avrg": 4  }
 ]
 },
 {
 "Name" : "Kitty 2",
 "RollNo": 102,
 "Percentage" : [
  {"num" : 68,
  "avrg": 4  }
 ]
 }
 ]

I am able to see Name and Roll no but not the percentage.num and percentage.avrg. Any thing that I am missing ?

Upvotes: 0

Views: 57

Answers (2)

Dazz
Dazz

Reputation: 629

Here is a working plunker

http://plnkr.co/edit/Eygjk9AkGrSm7KyGrnKR?p=preview

you have two options use percentage[0] like {{ kitty.Percentage[0].num}}

or change your Json to "Percentage" : {"num" : 88, "avrg": 4 }

Upvotes: 1

messerbill
messerbill

Reputation: 5629

percentage is modeled as an array. Either use an Object or you have to access it on another way like

 percantage[0].num 

but i think i would structure the json like:

{
"Name" : "kitty 1",
"RollNo": 101,
"Percentage" : 
{"num" : 88,
  "avrg": 4  }
 },
 {
 "Name" : "Kitty 2",
 "RollNo": 102,
 "Percentage" :
  {"num" : 68,
  "avrg": 4  }
 }
 ]

not tested, but in my opinion the combination of array AND objects always makes problems...

Upvotes: 2

Related Questions