Having issues with using jsonplaceholder.typicode.com as a data source in AngularJS

I am facing a challenge with my AngularJS project focused on creating a Users application.

Instead of storing the users' data array within the controller, as I have already accomplished successfully in this example (jsFiddle), I aim to utilize jsonplaceholder.typicode.com as a data source:

var root = 'https://jsonplaceholder.typicode.com';

// Defining an Angular module named "usersApp"
var app = angular.module("usersApp", []);

// Setting up a controller for the "usersApp" module

app.controller("usersCtrl", ["$scope", function($scope) {

  $scope.users = root + "/users";

}]);
.search-box {
  margin: 5px;
}

.table-container .panel-body {
  padding: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container" data-ng-app="usersApp">
  <div class="panel panel-default table-container">
    <div class="panel-heading">Users</div>
    <div class="panel-body" data-ng-controller="usersCtrl">
      <div class="row">
        <div class="col-sm-12">
          <div class="form-group search-box">
            <input type="text" class="form-control" id="search" placeholder="Search User" data-ng-model="search">
          </div>
        </div>
        <div class="col-sm-12">
          <table class="table table-striped table-bordered" id="dataTable">
            <thead>
              <tr>
                <th>Full name</th>
                <th>Email</th>
                <th>City</th>
                <th>Street</th>
                <th>Suite</th>
                <th>Zipcode</th>
              </tr>
            </thead>
            <tbody>
              <tr data-ng-repeat="user in users|filter:search">
                <td>{{user.name}}</td>
                <td><a href="mailto:{{user.email}}">{{user.email}}</a></td>
                <td>{{user.address.city}}</td>
                <td>{{user.address.street}}</td>
                <td>{{user.address.suite}}</td>
                <td>{{user.address.zipcode}}</td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>

However, despite my efforts, it seems that my approach is not functioning correctly. Any insights on what might be going wrong would be greatly appreciated. Thank you!

Update: Check out the working fiddle HERE.

Answer №1

 var root = 'https://jsonplaceholder.typicode.com'
    $scope.users = root + "/users";

Currently, no HTTP request is being made to fetch the data. The variable "users" in the scope is simply a string.

 $scope.users =  'https://jsonplaceholder.typicode.com/users'

To properly make an HTTP request and retrieve data, the code should resemble this:

var root = 'https://jsonplaceholder.typicode.com';

// Define an Angular module called "usersApp"
var app = angular.module("usersApp", []);

// Create a controller for the "usersApp" module
app.controller("usersCtrl", ["$scope","$http", function($scope,$http) {
  var url = root + "/users"
  
  // Using the $http service in Angular for REST calls
  // Make a GET request to the URL to get the data, which is resolved as a promise
  $http.get(url)
 .then(function(data){
    // Once the data is fetched, assign it to the scope
    $scope.users = data.data;
 })
}]);

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

What is the method for retrieving hotels from a database based on their proximity to a specific set of latitude and longitude coordinates?

I have a database table with latitude, longitude, and hotel locations. I want to create a feature that will show hotels near a specific point defined by latitude and longitude. Code Snippet function findNearbyHotels() { $this->lo ...

li experiencing a background width problem due to extended text

Check out this code snippet with a problem In the image below, you can see that the background of the li element with more text is larger compared to the others. It's important to note that the <ul> is scrollable. I've experimented with va ...

Tips on conducting a statistical analysis without having to wait for the completion of an AJAX response

I am looking to track the number of clicks on a specific div with the id #counter and then redirect the user to a different website. To achieve this, I have implemented an ajax call on the click event of the #counter div. Upon successful completion of the ...

I need to retrieve data from two different databases. What is the best way to combine the code into a single function?

I am working on fetching data from 2 database sources and combining them to send the merged data to result.html function showResult(req, res){ var n = req.query.query mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE ...

Just initiate an API request when the data in the Vuex store is outdated or missing

Currently, I am utilizing Vuex for state management within my VueJS 2 application. Within the mounted property of a specific component, I trigger an action... mounted: function () { this.$store.dispatch({ type: 'LOAD_LOCATION', id: thi ...

The error message indicates that the element countdown is missing or does not exist

I typically refrain from discussing topics that I'm not well-versed in (my weak spot has always been working with time and dates in javascript), but I find myself in urgent need of assistance. I've decided to use this link to showcase a countdow ...

Typescript enhances React Native's Pressable component with a pressed property

I'm currently diving into the world of typescript with React, and I've encountered an issue where I can't utilize the pressed prop from Pressable in a React Native app while using typescript. To work around this, I am leveraging styled comp ...

Attempting to design an interface in Angular 2 to align with the anticipated API response structure

I have a specific JSON response in mind that I am expecting from an API. My goal is to create a user interface for it. { items: [ { identifier: 1, details: [ { id: 1001, perishable: 0 }, { ...

The element type provided is not valid: it should be a string (for built-in components) or a class/function. Utilizing SSR with Node.js, React, and React-

Playground: https://codesandbox.io/s/focused-dream-ko68k?file=/server/server.js Issue Error: Encountered an error stating that the element type is invalid. It was expecting a string or a class/function, but received undefined instead. This could be due ...

Enhancing the appearance of the content editor with a personalized touch

I am working with a standard content editor that utilizes an iFrame as the text area. Upon changing dropdown options, it triggers the following command: idContent.document.execCommand(cmd,"",opt); Where "idContent" refers to the iFrame. One of the dropd ...

What is the method for invoking an anonymous JavaScript object when its name is unknown?

I've been dynamically loading JavaScript modules with jQuery's getScript function. I want to be able to call an init method that all of these modules contain. Since the modules are loaded dynamically, I would rather not hard code their names. Is ...

Retrieve the response text using native JavaScript

Dealing with the nature of asynchronous XMLHTTPRequest in JavaScript can be tricky. I faced a challenge where I needed to return the responseText value but found it impossible due to this nature. To overcome this, I came up with a workaround by calling a f ...

Using Ajax for updating table content on a JSP webpage section

I am working on implementing Ajax functionality for a table to enable partial updates every hour. Below is a snippet of my JSP code: < head > < meta http - equiv = "Content-Type" content = "text/html; charset=ISO-8859-1" > < titl ...

The construction was unsuccessful due to errors in the webpack process

I encountered a sudden error in my Next.js app. Is there any solution available to resolve this issue? ./pages/_app.tsx Error: [BABEL] C:\Projects\skribeNew\app-web\pages\_app.tsx: You provided us with a visitor for the node type T ...

Error: The function $(...) does not contain a progressbar

I encountered a TypeError saying $(...).progressbar is not a function when loading the Gentelella - Bootstrap Admin Template Page. This error is affecting many jQuery processes. How can I resolve this? Error in my Code: (custom.js) // Progressbar if ($( ...

Warning message will appear before navigating away from the page if vee-validate is

Wondering how to create a simple confirmation prompt asking if the user really wants to leave a page that includes a basic HTML form. The HTML Form: <!DOCTYPE html> <html> <head></head> <body> <div id="app"> ...

Steps for modifying an element in an array using Javascript

Currently, I am a beginner in the realm of JavaScript and I am trying to build a todo-style application. So far, I have successfully managed to create, display, and delete items from an array. However, I am facing challenges when it comes to editing these ...

Sorting `divs` based on the number of user clicks in JavaScript: A simple guide

I've implemented a script that tracks the number of clicks on each link and arranges them based on this count... Currently, everything is functioning correctly except when the <a> tags are nested inside a div. In such cases, the script ignores ...

Arranging arrays of objects based on their occurrence rate

I am working with an array of objects that looks like this: students = [ { "name": "Ana Barbique", "category":"B" } { "name": "Marko Polo", "category":"B" } { "name": "Nick Harper", "cate ...

JavaScript event array

I have a JavaScript array that looks like this: var fruits=['apple','orange','peach','strawberry','mango'] I would like to add an event to these elements that will retrieve varieties from my database. Fo ...