0

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 ?

1
  • 3
    Percentage is an array. Use Percentage[0].num Commented Jun 26, 2015 at 13:26

2 Answers 2

2

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...

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

Comments

1

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 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.