The ng-repeat function is currently disabled and not displaying any data from the JSON object

I am currently facing an issue where the ng-repeat Directive in my code is getting commented out and not displaying the results of the JSON object. I have verified that the object is being properly passed to "this.paises2" using the toSource() method, and everything seems to be fine.

I have checked various resources but haven't found a solution yet:

AngularJS: ng-repeat not displaying JSON

ng-repeat code getting commented out when viewed from browser

ngRepeat div get commented out when i see from the browser inspector

Here is the snippet of HTML code causing the issue:

<html ng-app="miModulo">

<head>
    <link href="mis-css.css" rel="stylesheet" type="text/css">
</head>

<body ng-controller="miControlador as vm">
    <button ng-click="vm.buscarEnRegion()">Pulsar</button>

    <ul ng-repeat="elemento in vm.paises">
    <li>
        {{elemento.name}}
    </li>
    </ul>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <script src="mijs1.js"></script>
</body>

And here is the corresponding Javascript/AngularJS code:

angular
.module("miModulo",[])
.controller("miControlador",['$http',funcionPrincipal] ); 

function funcionPrincipal($http) {
    this.buscarEnRegion=function(){
        $http.get("https://restcountries.eu/rest/v1/region/africa").then(function(miRespuesta){
            this.paises=miRespuesta;
            alert(this.paises.toSource());
        },function(miRespuesta){
            alert("incorrecto");
        });
    }
}

Any help or suggestions would be greatly appreciated. Thank you!

Answer №1

There are a couple of issues that need to be addressed, as outlined in the angular docs.

The response object contains these properties:

  • data – {string|Object} – The response body transformed with appropriate functions.
  • status – {number} – Representing the HTTP status code of the response.
  • headers – {function([headerName])} – A function for getting headers.
  • config – {Object} – Configuration object used for making the request.
  • statusText – {string} – The HTTP status text of the response.
  • xhrStatus – {string} – Indicates status of the XMLHttpRequest (complete, error, timeout, or abort).

In order to access the data you require, use miRespuesta.data.

The other issue arises when assigning this.paises=miRespuesta, where this refers to the function, not the controller. To resolve this, ensure you assign the variable to the controller. Here's how you can modify your controller accordingly:

function principalFunction($http)
{   
    var vm = this;
    vm.searchInRegion=function()
    {
        $http.get("https://restcountries.eu/rest/v1/region/africa").then(function(miRespuesta) 
        {
            vm.countries=miRespuesta.data;
        },function(response)
        {
            alert("Incorrect");
        }
        );
    }
}

To see an example, check out the plunker.

Answer №2

Modify this

<ul ng-repeat="elemento in vm.paises">
    <li>
    {{elemento.name}}
    </li>
</ul>

to the following

<ul >
    <li ng-repeat="elemento in vm.paises">
    {{elemento.name}}
   </li>
</ul>

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

Submit a POST request using CoffeeScript to get a string from the returned object

I am encountering a small issue. Whenever I execute myVar = $.post('/check_2/', JSON.stringify({"newname": window.NEWNAME,}), callback, 'json') The variable 'myVar' holds an object. When I use console.log myVar, the output i ...

Receive updates from the backend GraphQL and display them on the frontend React application while executing a mutation

I recently implemented a mutation call from the frontend (react) to the backend, running 5 SQL query files. While the backend processes the queries, the frontend simply shows a "loading..." message. //frontend <Button onClick={()=>addData()} /> C ...

Embed your JavaScript code within the header tags of the WooCommerce order confirmation page

Seeking to enhance my thank you page with Google tracking script, I have developed code that successfully injects the tracker within the of the page, incorporating dynamic values. However, my goal is to embed it within the tags instead. function mv_goog ...

Can the Web API Controller return custom JSON data without using escaping characters when using CreatedAtAction?

Is there a way to avoid ASP Net Core escaping self-created JSON when returning it from a controller action method? Take a look at this code snippet: [Route("api/[controller]")] [ApiController] [Produces("application/json")] public class MyController : Con ...

Filter ng-repeat using OR condition for property

I'm trying to filter a list based on a model value entered in a text box. For example: var person={}; person.Id=1; person.Name="Test 1"; person.PetName="Rest 1" var persons=[]; persons.push(person); person.Id=2; person.Name="Test ds"; per ...

Implementing $modal.open functionality in AngularJS controller using Ui-Bootstrap 0.10.0

Is there a way to properly call $modal.open from the controller in AngularJS since the removal of the dialog feature in ui-bootstrap 0.1.0? What is the alternative method available in the current version? In previous versions like 0.1.0, it was simply don ...

Obtaining the mouse position in JavaScript in relation to the website, preferably without relying on jQuery

I came across this code snippet on Ajaxian, but I am having trouble using cursor.y (or cursor.x) as a variable. When I call the function with it that way, it doesn't seem to work. Could there be a syntax issue or something else causing the problem? f ...

What distinctions can be observed between interacting with the Jira Rest API manually compared to through programming?

Just starting out with Jira Rest APIs...I'm looking to log in to my Local Jira server using a VBA program. While I can successfully access the REST API manually and receive the JSON response I need, I keep encountering an error whenever I try to do it ...

Utilize the click spinner feature on a single button in Angular

I have a table with a list of items and each item has a button to request a PDF document from the server. I want to show a spinner while waiting for the document to be received. Currently, when I click on a button, the spinner appears on all buttons instea ...

Access the contents of MUI Modal (Dialog) even when it's closed

I'm currently working on an app using Material-ui and firebase authentication. My goal is to integrate firebaseui's authentication within a MUI Dialog component. The issue I've encountered is that in order for the authentication component t ...

Exploring the method of accessing one Vue plugin from another plugin with the use of Vue.prototype

I'm currently working on developing a Vue plugin aimed at simplifying the management of authentication state throughout my application. This particular plugin will require interaction with other Vue plugins such as vuex, vue-router, and vue-apollo (fo ...

Querying the field's object type in MongoDB

My database contains records with a field that has different data types in each record. I would like to query only for the records where this field contains strings. Is there a way to search for specific data types in this field? {"field1":ObjectId("53de" ...

Decoding dynamic JSON data into a versatile .NET object

How can I effectively deserialize the JSON response provided below into a generic object? Specifically, I need to extract the response message and access the list of errors, along with the field name and error message associated with each error. { "mess ...

Creating interactive and adaptable maps

Currently, I am working on making a Joomla website responsive. However, I have encountered an issue with the SobiPro Map. The problem arises when displaying a map (background img) and points (a link + img) over it with an absolute position. As a result, wh ...

When the onClick method is triggered, it retrieves a single object from json_encode

Currently, I am using the json_encode method on data extracted from a table. After applying a var_dump to this variable, it reveals three objects: {"id": "5"} {"id": "6"} {"id": "7"} Here's my process: I have an image with an onclick function: < ...

Is it possible to establish a connection between React and a MySQL Database?

I've been encountering issues with connecting to a remote database in React. Despite my efforts, I haven't been successful in establishing the connection. I have tried various solutions without any luck. The goal is simple - I just want to connec ...

Modifying properties of an array of objects in React Native using JavaScript

I have a scenario where I am using Flatlist to render a couple of boxes. If the "shapes" element's "visible" key is false, the box will be blank. This visibility property is defined in state and I'm not sure if this is the correct approach. Now, ...

divs adjust their size based on how many are placed in a single row

I'm in the process of developing an online editing tool, and I'm interested to know if it's feasible to adjust the size of a <div> based on the number of visible div elements. For instance, I have a row with three columns, each set at ...

Implementing functionality: Removing a row and applying a CSS class upon button click

Check out the fiddle below: https://jsfiddle.net/shaswatatripathy/enq0kfqL/2/ I need help with creating a remove function and adding a CSS class to a clicked row. 1. When I click on "remove", the clicked row should be deleted. When I click on a row, ...

Ways to retrieve a value within a function and update a variable

Fetching data from the firebase database = firebase.database(); var ref = database.ref('urls'); ref.on('value', gotData, errData); function errData(err){ console.log('Error!'); console.log(err); } function gotData(d ...