The ng-repeat directive is not showing the data retrieved from the HTTP request. It appears as a comment when

My

homeController.js

var app = angular.module('myApp');
app.controller('HomeController', 
function($scope, $http, $rootScope, $stateParams, $state, LoginService) {
$scope.user = $rootScope.userName;
console.log("Starting http request");
$http.get("http://127.0.0.1:5000/trying").success(function (response) {
      $scope.myData = response.users;
      console.log(response);
  });
console.log("ending http request");
});
<div class="col-md-12" style="width:500px;">
    <div align="right"><a ui-sref="login">Logout</a></div>
        <h4>Welcome {{user}}! </h4>
        <p><strong>This is Home Page</strong></p>
        <ul>
        <li ng-repeat="x in myData">
            Data are : {{ x.fname + ', ' + x.coordinates }}
        </li>
        </ul>
    </div>

Console

Response object

The home.html page is being loaded from index.html as expected. The user data is showing correctly after logging in. However, the ng-repeat functionality seems to be not working properly. Upon inspecting, it appears that it's being commented out. What could be causing this issue?

Answer №1

  1. To ensure that $scope.myData is recognized by the view, consider defining it as an array or object outside of the http.get function, right below $scope.user. This way, the view will know what to expect.
  2. Confirm whether myData is an array or object and use the appropriate loop syntax (x in myData vs x of myData). Another option is (dataKey, dataValue) in myData.
  3. For content dependent on an http call, add ng-if="!isLoading" to ng-repeat. Initialize isLoading as true in this.$onInit function and set it to false in the response after the http get request completes.

`

this.$onInit = function(){
  $scope.isLoading = true;
  // rest of the stuff like http.get...
  $http.get("http://127.0.0.1:5000/trying").success(function (response) 
    $scope.myData = response.users;
    $scope.isLoading = false;
    console.log(response);
  });
};

`

If you're still experiencing issues with your view's behavior, let me know. It's possible there may be another reason causing the view not to update.

PRO TIP: Ensure that the initial definition of $scope.myData matches the type of variable being returned. Avoid declaring as [] but later filling in as {} with an api call.

This issue can be challenging since vanilla JavaScript allows for variable type swapping without strict enforcement. Some developers prefer TypeScript or other JavaScript alternatives for more concrete typing rules. But that's a topic for another discussion entirely.

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

Be mindful of potential missing dependencies when utilizing event listeners and states within useEffect

Whenever I utilize the addEventListener() method alongside trying to access some state within the useEffect, I consistently face the same issue. Adding the state as a dependency is not an option, as that would result in multiple event listeners being creat ...

How can I configure Grails to properly interpret my JSON PUT AJAX request?

In this Grails app, the form is set up in a standard way. <g:form url="[resource:myClass, action:'update']" method="PUT" > <fieldset class="form"> <g:render template="form"/> </fieldset> <fieldset c ...

Why does a boolean value behave this way in JSX, specifically when used in a checkbox scenario?

I am completely new to React/JSX and I'm struggling to grasp the logic behind how things function. For instance, <input type="checkbox" checked=true /> results in <input type="checkbox checked />. On the other hand, <inp ...

Unable to detect ajax request in CakePHP 2x

I am facing an issue with an API on one server and a view file on another server. The problem arises when I make an AJAX request, as the value for $this->request->is('ajax') always returns false. Strangely, this setup works perfectly on m ...

Discover the best practices for utilizing CSS selectors reliably in puppeteer

For a project, I am currently working on customizing a puppeteer script that is able to play a song from Soundcloud and record it. The main goal is to utilize a CSS selector to display the duration of the song as well. I am encountering difficulties with g ...

jQuery sending a GET request but receiving null parameters in an MVC controller

Currently, I am utilizing jQuery to perform a GET request. The method being called resides in my MVC-4 controller and expects one parameter. After verifying that both my data and the JSON format are valid: { "txid": "051e30921f2886595ad9f22401437f10a ...

Play a diverse selection of audio variables at random

As I prepare to transition into the view, I would like the controller to select a random audio file and play it. I'm feeling a bit lost on where to even begin with this task. Controller: var audioOne = new Audio("img/1.mp3"); var audioTwo = new Audi ...

Ten instances of $digest() being triggered upon the implementation of custom filters

I am struggling with the following angular markup: <tr ng-repeat="dia in dias"> <td>{{ dia[0].fecha }}</td> <td ng-repeat="bloque in bloques"> <div ng-repeat="hora in dia|soloBloque:bloque|sacarHoras"> ...

Executing a Python script to run the main.js and require.js files

Looking for assistance on executing JS files with Python code. For instance, transforming this HTML snippet: <script data-main="main.js" src="require.js"></script> into Python. ...

The Protractor actions().mouseMove function does not seem to function properly in Firefox and IE, although it works perfectly in Chrome

I've encountered an issue with the mouseMove command while using the actions class. The error occurs specifically when running the script on Firefox and IE, but works fine on Chrome. Below is the code snippet I attempted: browser.get("https://cherch ...

Error in Firefox: The Ajax request was not sent for unknown reasons

In the process of making a synchronous GET ajax request in Firefox 27.0.1, Fedora 20, and using jQuery 1.11.0: $.ajax(ajaxParam).then( function (r) { html = r.html; }, function (jqXHR) { console.log(JSON.stringify([jqXHR, $.aja ...

Looking to achieve a mouse over effect in jQuery?

For the past few days, I've been grappling with a question that I just can't seem to find the right answer to. I'm trying to create a mouseover effect similar to the one on this template (the Buddha at the top of the page). Despite my best e ...

Extract the input value from the bootstrap-datepicker using JavaScript

I'm working on a Ruby-on-Rails project and I want to include a string from the bootstrap datepicker into a hidden field. However, I am unsure of how to reference an input class in this process. Below is the structure of my form: <%= simple_form_f ...

Upload files via Ajax request is required

I am in the process of trying to upload a binary file to a server while avoiding a full page refresh when the server responds. I must admit, I am not well-versed in this area and I understand if my approach needs some adjustments. This is how I have appro ...

Verifying WordPress slug existence using AJAX

I want to verify if a slug URL already exists using the user interface. My initial idea was to use an AJAX solution similar to this. `jQuery("#slugBrut").keyup(function() { var slugBrutText = jQuery("#slugBrut").val() ; ...

Ways to extract the ID by iterating through buttons

I encountered a message in my browser while looping through buttons with onclick function(). Are there any alternative solutions? Error handling response: TypeError: self.processResponse is not a function at chrome-extension://cmkdbmfndkfgebldhnkbfhlneefd ...

Interactive jQuery slideshow showcasing top content

I'm experiencing an issue with displaying the initial content in a loop of divs that show their content sequentially after 5000 milliseconds. Is there a simple solution to make the first content area display immediately, followed by the rest sliding ...

A complex valueOf function in Javascript

What is the purpose of using ({}).valueOf.call(myvar)? This expression converts any value to an object. If the input is already an object, it remains unchanged; however, if it is a primitive type, it gets converted to an instance of a wrapper type. I ...

"Exciting Changes in Color According to the Active State of vue-route-link

I am trying to find a way to customize the CSS based on whether a link is exact or active. Essentially, when a user clicks on a menu item, I want the underline to change depending on whether the link is an active router-link. Although I was able to accompl ...

Retrieve the result of the Object match operation

Although I'm not an expert in JavaScript, please bear with me as I ask some basic questions. I've been struggling to find a suitable answer to my query. I am currently working on my first Node.js application, incorporating Extra Framework and So ...