Angular JS struggling to load JSON data

As a beginner in the world of AngularJS and JSON, I decided to test my skills with the following example. However, I am encountering some difficulty loading data from a 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  }
 ]
 }
 ]

Although I can view the Name and Roll number, I'm unable to see the values for percentage.num and percentage.avg. Is there something crucial that I might be overlooking?

Answer №1

Percentage data is structured as an array. To access it, you can either use an object or navigate to it in a different way such as

 percentage[0].num 

Personally, I would organize the JSON like this:

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

This setup has not been tested, but I believe that using a combination of arrays and objects can lead to complications...

Answer №2

Check out this functional plunker example:

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

You have a couple of choices: you can either utilize the percentage[0] like {{ kitty.Percentage[0].num}}

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Setting focus on the require attribute for the <input> tag in a React application

I have a login/register form with multiple input tags. My goal is to automatically set focus and require attribute on the first input tag when the form is opened. I have tried using jQuery and JavaScript to add the required attribute, which works fine. H ...

Looking to implement a star rating feature in Vue.js 2?

My perspective is as follows : <div class="col-md-8"> ... <star-rating :value="3"></star-rating> ... </div> This is how my star-rating component looks like : <template> <span class="rating"> &l ...

The jQuery scrollTop feature seems to be malfunctioning

My code is causing an error that I don't understand. Any help would be appreciated... I'm getting the following error message: Property does not exist on type '.js--section-plan'.ts(2339) when I hover over the offset() in vscode $(&apos ...

Utilizing a query as a parameter in a query string

Utilizing an API with poor documentation can be challenging, especially when trying to decipher the best practices for its usage. This particular API endpoint supports query parameters such as 'query' (string) and 'pageNumber' (integer) ...

The request response was a JSON object that started with the keyword "POST"

My frustration is growing as I encounter a strange issue with the stack template I purchased from envato market. Every time I attempt a simple ajax request, the json returned from my PHP script seems to be invalid. Interestingly, the exact same code works ...

Is it possible to dynamically return JSON structures of varying types?

I've been experimenting with Go and facing challenges when trying to adapt my inheritance-based designs from other languages to its structure. While I have experience coding in OCaml and similar languages with a familiar structure, the absence of type ...

Leveraging TypeScript's "this" keyword within an interface

I am currently working on a personalized interface where I aim to determine the type of an interface value within its implementation rather than in the interface definition, without using generics. It is important to note that these implementations will al ...

Class variable remains unchanged following AJAX request

Upon completion of the ajax request, two integers are received - this.N and this.M. However, these values are not being set by the storeDims() function even though the correct decoding is done on the dims variable. This implies that I am unable to access ...

Type parameter for unprocessed JSON information

I am currently facing an issue with a script that communicates with my API and returns the response to the caller. I am having trouble defining the type correctly for this operation. The responses from the API always consist of a boolean flag, success, in ...

Is there a way to retrieve all the checked objects from a group of AngularJS Checkboxes?

How can I retrieve all the selected objects from checkboxes using AngularJS? Here is a snippet of my code: View Template (view.tpl.html) <tr ng-repeat="item in itemList"> <td> <input type="checkbox" ng-click="clickedItem(item.id)" ...

Transforming a string of JSON with unique characters into a jsonObject

I need to process a jsonString that contains special characters. The goal is to remove these special characters in order to obtain a proper jsonString and then convert it into a jsonObject. String text = "some example json string with special characters... ...

Using jQuery to loop through a table and choose dropdown menus

My task seemed simple at first. I have a table of drop-down lists, with a text box at the end of each line. All I need to do is loop through each row of the table, retrieve the value from the "hours dropdown" and multiply it by the value from the "rate d ...

Discover the best method for navigating through ZOHO People JSON data in VB.NET

After making a call to the Zoho People API from VB.NET, I received the following code and JSON response. My task now is to parse through the values and create insert SQL queries. { "result": [ { "507258000002665141": [ { "Cre ...

Can you eliminate data errors within a Vue component?

In my Vue instance, I will be making a multitude of API calls to modify various variables. The initial value of these variables is not crucial. I had hoped there might be a way to avoid defining them upon the creation of the vm, but this approach doesn&ap ...

Separating views, collections, and models in Backbone.js into different JS files can lead to issues where they may not be able to communicate with each other effectively

I have successfully created a web app using Backbone.js where all the views, collections, and models are written into one js file. Now, I want to separate them into different js files like this: <script type="text/javascript" src="js/layermanagemodel. ...

Scrollspy mistakenly targeting the incorrect element

Upon examining this example, it is evident that Bootstrap Scrollspy is encountering issues with the navigation and not functioning as intended. The incorrect item is being assigned the .active class. $('.spycontent').scrollspy({ target: ' ...

Issue with loading Angular CSS files arises when managing two partials using a single controller

I am currently developing a web application using the MEAN stack. My project utilizes Bootstrap as the CSS library, with some CSS overrides present in two of my own files named "app.css" and "mio.css". Everything was running smoothly until I encountered an ...

How can I retrieve the width of a responsive React element during its initial rendering phase?

In my React project, there is a component called ResultList which is used to display products in a gallery format. The challenge I'm facing is determining the appropriate number of products to show per row based on the available width for the ResultL ...

Updating values in an Object by assigning them with results from an array

I've been grappling with a data structure issue in nodejs and I could really use some assistance. Here's the scenario: I have an object: let obj = { commenter: '', comment: '', numberOflikes: 0, } Along with a containe ...

How can we effectively streamline these if statements for better organization and efficiency?

Currently, I am dealing with a straightforward if condition structure and aiming to keep the code as DRY (Don't Repeat Yourself) as possible. I believe that I have achieved optimal dryness for my specific situation by utilizing object keys as pointers ...