i have trouble displaying json data in angular. send data backend frontend (angular), cannot display them.
i tried simulate similar situation on jsfiddle, although have prepared data backend 
get/send data -> backend side:
//push data 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 redis , send frontend app.get('/messages', function(req, res){ // query redis dataset here redisclient.lrange('list', 0, -1, function(err, messages) { console.log(messages); // handle errors if occur if(err) res.status(500).end(); // send string res.send(messages); // or json // res.json({ data: reply.tostring() }); }); }); get data -> frontend (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 ng-repeat directive:
<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 know problem is?
as far can see, you're storing object json, never parse it. therefore using
$scope.messages = json.parse(res); instead of
$scope.messages = res; should fix problem.
here working jsfiddle version of yours: https://jsfiddle.net/29y61wtg/5/
note, doesn't include $http call, if you're still having problems after using $http, tell me in comments.
Comments
Post a Comment