The ng-repeat directive seems to be malfunctioning in AngularJS 1.6

Recently diving into AngularJS, I opted for version 1.6 to fetch data from my database. Everything seems to be in place, but the JSON information fails to display.

This is the snippet of my code:

<div class="row m-t-50">
    {{ autos |json }}
    <div class="col-md-12">
        <table class="table table-striped">
            <thead>
                <tr>
                    <th>Marca</th>
                    <th>Modelo</th>
                    <th>Color</th>
                    <th>Año</th>
                    <th>Precio</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="auto in autos">
                    <td>{{ auto.marca }}</td>
                    <td>{{ auto.modelo }}</td>
                    <td>{{ auto.color }}</td>
                    <td>{{ auto.anio }}</td>
                    <td>{{ auto.precio }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

The output of {{ autos | json }} appears as follows:

{
    "data": [{
        "id": "1",
        "marca": "TOYOTA",
        "modelo": "VC2017",
        "color": "Verde",
        "anio": "2017",
        "precio": "250345"
    }, {
        "id": "2",
        "marca": "NISSAN",
        "modelo": "NS2017",
        "color": "Azul",
        "anio": "2016",
        "precio": "540000"
    }],
    "status": 200,
    "config": {
        "method": "GET",
        "transformRequest": [null],
        "transformResponse": [null],
        "jsonpCallbackParam": "callback",
        "url": "php/obtener-autos.php",
        "headers": {
            "Accept": "application/json, text/plain, */*"
        }
    },
    "statusText": "OK"
}

Despite having the JSON data, the displayed table remains empty. Any idea where I might have gone wrong?

Answer №1

Utilize the ng-repeat directive on

<tr ng-repeat="auto in autos">
. Based on the provided data, the iteration should be applied to the autos.data array.

Modify it as:

<tr ng-repeat="auto in autos.data">

OR

Within the controller, assign the data from the response to the autos variable.

$scope.autos = response.data;

Then use it in the view as follows:

<tr ng-repeat="auto in autos">

The object named autos represents the result of an $http request. This response includes a property called data for accessing the actual data transmitted from the server. To retrieve this data, utilize response.data.

Additional properties available are status – status, headers, config, and statusText.

Answer №2

For optimal results, utilize the autos.data method.

EXEMPLIFICATION

var app = angular.module('todoApp', []);

app.controller("dobController", ["$scope",
  function($scope) {
     $scope.autos ={"data": [ { "id": "1", "marca": "TOYOTA", "modelo": "VC2017", "color": "Verde", "anio": "2017", "precio": "250345" }, { "id": "2", "marca": "NISSAN", "modelo": "NS2017", "color": "Azul", "anio": "2016", "precio": "540000" } ], "status": 200, "config": { "method": "GET", "transformRequest": [ null ], "transformResponse": [ null ], "jsonpCallbackParam": "callback", "url": "php/obtener-autos.php", "headers": { "Accept": "application/json, text/plain, */*" } }, "statusText": "OK" };
   
  }
]);
<!DOCTYPE html>
<html ng-app="todoApp">

<head>
  <title>Sample</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular.min.js"></script>
</head>
<body ng-controller="dobController">
   <div class="row m-t-50">
{{ autos |json }}
<div class="col-md-12">
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Brand</th>
                <th>Model</th>
                <th>Color</th>
                <th>Year</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="auto in autos.data">
                <td>{{ auto.marca }}</td>
                <td>{{ auto.modelo }}</td>
                <td>{{ auto.color }}</td>
                <td>{{ auto.anio }}</td>
                <td>{{ auto.precio }}</td>
            </tr>
        </tbody>
    </table>
</div>
</body>

</html>

Answer №3

  <body ng-controller="MyCtrl">
      <div>
        <div ng-repeat="d in data"> {{ d.marca }}</div>
      </div>
  </body>

Check out the live version on Plunker

Answer №4

For Angular version 1.6.1, you can refer to this example:

Your HTML:

<table class="table table-striped">
        <thead>
            <tr>
                <th>Brand</th>
                <th>Model</th>
                <th>Color</th>
                <th>Year</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="car in cars">
                <td>{{ car.brand }}</td>
                <td>{{ car.model }}</td>
                <td>{{ car.color }}</td>
                <td>{{ car.year }}</td>
                <td>{{ car.price }}</td>
            </tr>
        </tbody>
    </table>

Your code:

$http.get("your url").then(function (response) {
            $scope.cars= JSON.parse(response.data);
        });

Make sure to include JSON.parse(response.data), as it is required in version 1.6.1.

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

Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home p ...

What could be causing the VueJS function not to be triggered by the dropdown selection?

I created a simple component called LocaleSwitcher.vue using the Element UI library: <template> <el-dropdown trigger="click"> <div> <i class="el-icon-map-location"></i> </div> < ...

Searching for the actual "value" of ng-href - a mysterious journey into the world of AngularJS!

What is the best way to retrieve the value of pk from the ng-href attribute and utilize it in my controller.js? ...

What is the best way to retrieve recently inserted data using Sequelize in PostgreSql?

Is there a way to retrieve updated table values after adding a user to the "WOD" table without making an additional query? Currently, when I add a third user to my WOD table, I can only return the first two users because I am unable to access the updated ...

the state hooks are failing to update the state when there is a change

Struggling with updating the title of my blog in a simple blog app. The current state is not being updated despite multiple attempts to change the method of setting state. Here's a snippet from App.js: function BlogDetail() { const [blogName, set ...

Discovering the overlap between two arrays using Node.js

Question: Simplest code for array intersection in JavaScript In the development of my app using Mongodb and Nodejs, I am working with a 'students' collection that includes an array listing all the courses (course IDs) a specific student has ta ...

Using ReactJS and Ruby on Rails to implement an AJAX delete request

There is a component named Items residing inside a parent component called ItemsContainer. A click on a button within the Items component triggers an Ajax function to delete that specific Item. However, I am encountering a 500 error message at the moment ...

Convert Ajax null value to NoneType in web2py

Every time I save information on a page, an AJAX request is sent with all the necessary data to be stored in the database. The format of this data looks similar to this example: {type: "cover", title: "test", description: null, tags: null} However, when ...

Error Encountered in .then Method with Vue.js

I am working on a GET request that determines whether the user is an Admin or not. The challenge I am facing is that I need to show a button using the v-if directive to verify if the value is true or false. By default, the value is set to false. Informati ...

Learn the process of crafting a higher order function within a functional component in React

I am currently working on creating a high-order component using React functional component, but I seem to be encountering an issue where I am not receiving the props value in the passed component. I am also incorporating TypeScript into my project. This i ...

How can we distinguish between the two samples?

Example A: function mergeAndSort(array){ if (array.length > 1){ let middle = parseInt(array.length/2); let leftArr = array.slice(0, middle); let rightArr = array.slice(middle); mergeAndSort(lef ...

Triggering a Javascript load event after injecting HTML

Is there a way to detect when injected HTML, such as an iframe containing a video, finishes loading using JavaScript? I attempted to add JavaScript at the end of the injected content, but it is not running as expected. Furthermore, I prefer not to use jQ ...

The function 'append' is not defined for the HTMLDivElement object when using Internet Explorer

Here is a snippet of code to create a new div element: var divElement = document.createElement('div'); divElement.setAttribute("id", "testId"); element[0].append(divElement); In this code, the 'element' variable represents $element fr ...

The scrollBy function is disabled when the user is already scrolling (iOS only)

I am facing an issue with a list of items that gets a new item added to it at regular intervals, resembling a data feed. The list is supposed to update in real-time as new items come in, but if the user scrolls down, it should stay in place to enhance read ...

Numerous functionalities within the Angular configuration

Currently facing a hurdle in my first Angular project. The issue lies in implementing two functions in the Angular config. First function is for Facebook connect using ngFacebook Second function is for routing with ui-router The challenge arises when b ...

How to display a name in an iframe using React

When I retrieve the movie name in React, it appears correctly as {movie.name} / {movie.enname} ({years}) . However, when I try to display this name within an iframe window at https://example.com/movie/{movie.name}, it does not show up properly. Here is th ...

In an empty JavaScript MVVM Organization View, the ViewModel is packed with lines of code

I find myself always placing all of my code within the ViewModel because nothing else seems to fit. What kinds of things should be included in the view? Does anyone have any examples? ...

I'm wondering why my images aren't fading out correctly

Today, I delved into JQuery and JavaScript for the first time. It's possible that I've made a very basic mistake. Essentially, I have a section in the markup, where JavaScript sets the height to match the viewport. Inside this section, there are ...

Steps to store a string with all characters in a JavaScript variable

I am faced with a situation where I need to pass variables that are considered Javascript objects, in a format similar to the following: var pageVars= [ { origin:'page', property:'name', value:'whatever' }, { origin:& ...

The checkbox labeled "Shipping Same as Billing" is not functioning correctly. I have included my JavaScript code below, can someone please help me identify what I am overlooking? (Only JavaScript code is provided; HTML is

I'm having trouble transferring data from a 'shipping' address to the 'billing' address section. I've included all my code, but nothing seems to copy over when the check box useShip is selected. All the HTML code is provided f ...