I am attempting to display a JSON array in an angular application

How can I display a JSON Array on the UI using AngularJS?

I have the JSON array in the controller, and I'm attempting to retrieve it in the view. Below is the code snippet of what I have implemented.

Please provide feedback on whether my approach is appropriate or if there is a better way to accomplish this task.

var practiceApp = angular.module('usermodel', [])
.controller('UserController', UserController);

function UserController(){
this.users= [{
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  ...
]
<h1> Here we have listed two users</h1>
<body>
<div ng-model="usermodel" ng-controller="UserController as us" >
<table class="table table-striped">
<th>id</th>
<th>name</th>
<th>email</th>
<th>address</th>
<th>phone</th>
</thead>
    <tbody>
    <tr ng-repeat= "item inusers">
    <td>{{item.id}}</td>
    <td>{{item.name}}</td>
    <td>{{item.email}}</td>
    <td>{{item.address.city}}</td>
    <td>{{item.phone}}</td>
    </tr>
</tbody>
</table>
</div>
</body>
I would greatly appreciate any assistance.
Thank you!

Answer №1

Here is the working solution for your reference.

var App = angular.module('App', [])

  App.controller('UserController', function($scope) {

    setTimeout(function(){
      $scope.$apply(function(){ 
        $scope.Users = [
        {
          "id": 1,
          "name": "Leanne Graham",
          "username": "Bret",
          "email": "[email protected]",
          "address": {
            "street": "Kulas Light",
            "suite": "Apt. 556",
            "city": "Gwenborough",
            "zipcode": "92998-3874",
            "geo": {
              "lat": "-37.3159",
              "lng": "81.1496"
            }
          },
          "phone": "1-770-736-8031 x56442",
          "website": "hildegard.org",
          "company": {
            "name": "Romaguera-Crona",
            "catchPhrase": "Multi-layered client-server neural-net",
            "bs": "harness real-time e-markets"
          }
        },
        ...
        ]
      
      });
},100);

});
<html>
<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body>
  <div class="container" ng-app="App" ng-controller="UserController">

    <table class=" table table-striped table-bordered">
      <tr>
        <td>
          Id
        </td>
        <td>
          Name 
        </td>
        <td>
          Email
        </td>
        <td>
          Address
        </td>
        <td>
          Phone Number
        </td>
      </tr>
      <tr  ng-repeat="item in Users">
        <td>{{ item.id}}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.email }}</td>
        <td>{{ item.address }}</td>
        <td>{{ item.phone }}</td>
      </tr>
    </table> 
  </div>

Answer №2

<tr ng-if= "entry inData"> You must change it to

<tr ng-if= "entry in DataList">

Answer №3

Hey there! I noticed some errors in your code but don't worry, I've made the necessary corrections.

Take a look at the updated code below:

var practiceApp = angular.module('usermodel', [])
.controller('UserController', ['$scope',function($scope){
  
  var self = this;
  
  self.users = [{
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06556f6865637463466776746f6a28646f7c">[email protected]</a>",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92c1faf3fcfcf3d2fff7fefbe1e1f3bce6e4">[email protected]</a>",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
  // Additional users truncated for brevity
]
  
}]);
<h1> Here we have listed two users</h1>
<body>
<div ng-app="usermodel" ng-controller="UserController as us" >
<table class="table table-striped">
<th>id</th>
<th>name</th>
<th>email</th>
<th>address</th>
<th>phone</th>
</thead>
    <tbody>
    <tr ng-repeat= "item in us.users">
    <td>{{item.id}}</td>
    <td>{{item.name}}</td>
    <td>{{item.email}}</td>
    <td>{{item.address.city}}</td>
    <td>{{item.phone}}</td>
    </tr>
</tbody>
</table>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
<script src="user/userController.js" type="text/javascript"></script>
 <!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

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

I'd love some clarity on the concept of stacking contexts

After encountering a bug with a non-clickable video player inside a jquery ui dialog, I resolved the issue by changing position:relative; to position:inherit; Other solutions included removing position:relative; altogether or adjusting the z-index of the ...

Are certain countries legally requiring website accessibility? What are the repercussions for not meeting accessibility standards?

Are there countries with mandatory requirements for website accessibility? What are the consequences if a website is not made accessible in a country with such rules? Can the government take action like removing or blocking the IP address if a site ...

The package.json file is missing the "exports" main, causing the Error [ERR_PACKAGE_PATH_NOT_EXPORTED] to be displayed

Can anyone help me resolve the 'Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in package.json' issue? I've been struggling to fix it despite receiving assistance. The problem is quite baffling and the more I dwel ...

Angular's ability to support multiple controllers

Help needed! I am new to Angular and struggling to apply multiple controllers to my table. These controllers belong to different modules, specifically from the npm install module called angular-table-resize. However, the table resize feature doesn't s ...

A guide on incorporating an unflattened SVG file into a React component

I am having trouble embedding an SVG file in a React component because it seems to appear flattened. Is there a way to display it as pure SVG? Please take a look at where it is posted: Also, this is how it's supposed to be viewed: This is the metho ...

Looking for guidance on writing a JASMINE test case for my method. I've been struggling to mock the event listener. Can you please direct me to a relevant link or share some mock examples

Looking to create a jasmine test for the code snippet below... refreshCacheIfNewVersionIsAvailable(); //Function to check for a new cache version on page load and refresh the app cache if newer files are available function refreshCacheIfNewVersionIsAvail ...

Error: Unable to access the applicant's ID as it is undefined

I'm currently facing an issue with passing parameters from server.js to humanresources.js in a login request. Although the params are successfully printed out in server.js, they appear as "undefined" once passed on to the function in human resources.j ...

Saga Redux fails to pass parameters during post requests

This is my first time using redux Saga to handle API requests. I have created an async function for a simple post request in the following way: const onLoginRequest = async (userName, password) => await fetch(`${loginApi}`, { method: 'POST', ...

Unlocking the full potential of Flexbox for your material design dashboard layout

I am looking to create reusable templates that feature a clean and minimalist flex-based CSS design. The template I am working on is for a dashboard containing five "cards" of varying sizes. Cells 2, 3, and 5 should be of equal width, approximately a quart ...

Unable to locate the type definition file for 'jquery'

After updating my NuGet packages, I encountered an issue where I can no longer compile due to an error related to the bootstrap definition file not being able to find the jquery definition file within my project. Prior to the update, the directory structu ...

Baffling Visibility Issue: iframe's Transparency Lost in Chrome and IE, Only Visible in Firefox

I have encountered a puzzling issue: http://jsfiddle.net/akosk/t2KvE/8/ HTML <div id='base'> <iframe id="myFrame" src="about:blank" frameborder="0" allowTransparency="true"></iframe> </div> <script id="conte ...

The `introJs()` API encounters issues when connected to an element within a jQuery modal dialog box

I am attempting to showcase an Intro.js tour on a specific element located within a <table>. This particular <table> is nested inside a dialog box created using jQuery UI. The rows of the table are dynamically inserted using JavaScript: while ...

The link-dark bootstrap class fails to have an impact

As a beginner in using bootstrap, I am creating my first simple website. However, I am facing an issue with the footer copyright section where the class "link-dark" is not taking effect. <!-- Copyright --> <div class="container-fluid ...

How to interact with AngularJS drop-down menus using Selenium in Python?

I have been working on scraping a website to create an account. Here is the specific URL: Upon visiting the site, you need to click on "Dont have an account yet?" and then click "Agree" on the following page. Subsequently, there are security questions th ...

Is it possible to dynamically change HTML content by utilizing a JSON file?

Looking to implement a JavaScript loop (using jQuery) that can dynamically populate the HTML file with content from a JSON file based on matching <div> ids to the JSON "id" values. The solution should be scalable and able to handle any number of < ...

Refresh the datatable using updated aaData

How do I automatically update the Datatable with new Json data? POST request is used to receive data, which is then sent to the LoadTable function in order to populate the datatable. function initializeTable(){ $("#submitbutton").on( 'click', ...

Exploring Ways to Navigate to a Component Two Steps Back in Angular

Let's say I have three routes A->B->C. I travel from A to B and then from B to C. Now, is it possible for me to go directly from C to A? ...

Troubleshooting server-side sorting issues with AJAX implementation, encountering problems with headers functionality

I'm encountering a problem: Some headers are not functioning properly to sort the table. For example, Brand and Model work as expected, but VehicleType only works once, and CarID and ReleaseDate never seem to work. Here is my index.php file: index. ...

Developing a one-of-a-kind jQuery plugin with customized CSS styling

I am trying to create a custom jQuery plugin that will control the CSS on targeted paragraphs. Despite my research, I have not found any articles explaining what I need. I attempted to write the following code snippet, but it did not work. Can someone tell ...

Problems encountered when applying box-shadow for text background across multiple lines

Lately, I've become accustomed to using box-shadow to add backgrounds to text headings. I've been employing a little trick of applying a box-shadow to a span element within h1 tags in order to create a unique "background with padding" highlight e ...