javascript - JSON 响应未在 Angularjs View 中显示

标签 javascript angularjs json node.js

我在使用 Angularjs 在网页上显示 JSON 响应时遇到问题,在 DevTools 上,我可以看到 GET 请求工作得很好并且可以获取所有数据,但是当涉及到在列表上显示它时,我得到的只是点.

我的 Controller :

 budgetApp.controller('DepensesListCtrl', ['$scope', '$http',
        function DepensesListCtrl($scope, $http) {
            $scope.depenses = [];

            $http.get('http://localhost:3000/api/depenses', {withCredentials: true}).success(function(data) {
                $scope.depenses = data;
            });

使用 ng-repeat :

<div >
    <div class="jumbotron text-center">
            <h1>depenses Page</h1>   
    </div>
    <ul ng-repeat="depense in depenses">
        <li>{{depense.depname}}</li>
        <li>{{depense.depcat}} </li>
    </ul>

结果: enter image description here

响应 JSON: enter image description here

我尝试使用警报进行调试,我发现我的 depenses 数组总是给出未定义

最佳答案

您的数据是一个对象,具有属性Depense,其中包含您要重复的数组

尝试:

$http.get('http://localhost:3000/api/depenses', {withCredentials: true}).success(function(data) {
       $scope.depenses = data.Depense;
                           // ^^^ property that contains array
});

或者:

<ul ng-repeat="depense in depenses.Depense">

关于javascript - JSON 响应未在 Angularjs View 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46862776/

相关文章:

javascript - Angular JS ng-if 在链接上

python - JSON API 标准的 Django Rest Framework 方法

javascript - 寻找一个允许从服务器部分获取 JSON 对象而不是合并它们的 Javascript 库

javascript - WebGL 在逐像素操作中使用库的速度是否更快?

javascript - 当我尝试提交 HTML 表单时显示错误 "Cannot POST"

javascript - Chrome 扩展麦克风捕获

Javascript/Jquery Excel 文件名

javascript - angularjs $http.get() 调用上的 catch() 不会抑制错误

javascript - 过滤对象数组/将数组转换为另一个对象

php - 使用 MySQL 删除表中的所有行?