【问题标题】:Display json into html table with angular用角度将json显示到html表中
【发布时间】:2016-12-02 03:42:38
【问题描述】:

我在表上部署 json 响应时遇到问题, 这是我的controller.js

var app = angular.module('angularjs-starter', []);
app.controller('MainCtrl', function($scope, $http) {

$scope.choices = [{id: 'choice1'}];
$scope.jsonData = {};

这是我的 controler.js 中的函数,我用我的表格内容发布数据和我的 php 响应

$scope.continue = function(choices)
{
var json = $scope.choices;

$http.post('php/calculador.php', json)
 .then(function(response) {

    $scope.jsonData = response;
    console.log($scope.jsonData);

});


};
});

我在控制台中打印了 json 数据以确保数据正确,但它没有在我的 html 表中显示任何数据

这是我的 HTML 表格 我试图在哪里部署我的 json

<div ng-app="angularjs-starter" ng-controller="MainCtrl">
<table>
<tr ng-repeat="x in jsonData">
<td>{{ x.costo_m }}</td>
<td>{{ x.desc }}</td>
<td>{{ x.id }}</td>
</tr>

这是打印在控制台上

Object
data:Array[1]
   0: Array[1]
      0: Object
        costo_m: 18.973529411765
        desc: "BLONG-F25+MOBICTRL"
        id: "choice1"
        licencias: 4.3

【问题讨论】:

    标签: html angularjs json


    【解决方案1】:

    你的jsonData 不是一个数组,它是一个对象,它似乎有一个属性data,然后它包含一个数组数组。不过,内部数组似乎是您想要的。

    所以,只需将 $scope.jsonData = response; 替换为 $scope.jsonData = response.data[0];

    【讨论】:

    • 哦,对不起,我忘记发布我已经尝试过了,没用:(
    • 我的错。我又看了一遍,实际上response.data 似乎不是一个数组,而是一个数组数组。我对数据本身了解不多,但您似乎需要response.data[0]。告诉我这是否正确,我会更新我的答案。
    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 2020-08-24
    • 2019-05-24
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多