node.js 解析 JSON 并在 Angular 中显示数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31415270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:58:08  来源:igfitidea点击:

Parse JSON and show data in Angular

jsonangularjsnode.jsexpress

提问by corry

I have a trouble with displaying JSON data in Angular. I successfully send data from backend to frontend (Angular), but I cannot display them.

我在 Angular 中显示 JSON 数据时遇到问题。我成功地将数据从后端发送到前端(Angular),但无法显示它们。

I tried to simulate a similar situation on JSFiddle, although I already have prepared data from backend following format

我试图在JSFiddle上模拟类似的情况,尽管我已经从后端准备了数据以下格式

get/send data -> Backend side:

获取/发送数据 -> 后端:

//push data to Redis
var messages= JSON.stringify(
    [
        {
            "name": "Msg1",
            "desc": "desc Msg1"
        },
        {
            "name": "Msg2",
            "desc": "desc Msg2"
        },
        {
            "name": "Msg3",
            "desc": "desc Msg3"
        },
        {
            "name": "Msg4",
            "desc": "desc Msg4"
        }
    ]);
    redisClient.lpush('list', messages, function (err, response) {
        console.log(response);
    });

//get from redis and send to frontend
app.get('/messages', function(req, res){
    // Query your redis dataset here
    redisClient.lrange('list', 0, -1, function(err, messages) {
        console.log(messages);
       // Handle errors if they occur
       if(err) res.status(500).end();
       // You could send a string
       res.send(messages);
       // or json
       // res.json({ data: reply.toString() });
    });
});

get data -> frontend (Angular)

获取数据 -> 前端(Angular)

angular.module('app')
    .controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
        'use strict';

        getFromServer();
        function getFromServer(){
          $http.get('/messages')
               .success(function(res){
                   $scope.messages= res;
                   console.log(res);
               });
        }
    }])

HTML part with ng-repeat directive:

带有 ng-repeat 指令的 HTML 部分:

<div ng-app="app" ng-controller="MainCtrl" class="list-group">
    <div class="list-group-item" ng-repeat="item in messages">
        <h4 class="list-group-item-heading">{{item.name}}</h4>
        <p class="list-group-item-text">{{item.desc}}</p>
    <div>
</div>

Would anyone know what the problem is?

有谁知道问题是什么?

回答by Florian Wendelborn

As far as I can see, you're storing your Object as JSON, but you never parse it. Therefore using

据我所知,您将对象存储为 JSON,但您从不解析它。因此使用

$scope.messages = JSON.parse(res);

instead of

代替

$scope.messages = res;

should fix your problem.

应该可以解决您的问题。

Here is a working JSFiddle version of yours: https://jsfiddle.net/29y61wtg/5/

这是你的一个有效的 JSFiddle 版本:https://jsfiddle.net/29y61wtg/5/

Note, that this doesn't include a $http call, if you're still having problems after using $http, tell me in the comments.

请注意,这不包括 $http 调用,如果您在使用 $http 后仍有问题,请在评论中告诉我。