【发布时间】:2015-09-18 18:41:19
【问题描述】:
我是 Web 开发的新手。如果这是一个愚蠢的问题,我为此道歉。我的 ng-repeat 没有显示从 mongodb 获取的 JSON,但是当我传递本地 JSON 时它可以工作。我为此奋斗了一整天。谁能告诉我出了什么问题?
这是我的 Angular 代码
(function(){
var app = angular.module('app', ['ngRoute']);
app.controller('CommentController', ['$scope', '$http', function($scope, $http){
//I've tried 'this.comment', it still not work.
//It works when I use a test local JSON'this.comment = [{Visitor: 123, Comment: 345, CreateDate: 879}, {Visitor: 123, Comment: 345, CreateDate: 879}]
$scope.comment =
$http({url: 'db-comments'}).success(function (comments) {
//I've confirmed 'comments' is an array of the JSON objects which I want.
return comments;
});
}]);
})();
这是我的 HTML 代码
<div id="home" class="container" ng-controller='CommentController as comments'>
<div id="comment" ng-repeat="x in comments.comment">
<h2>{{x.Visitor}}</h2>
<hr>
<p>{{x.Comment}}<p>
<span>{{x.CreateDate}}</span>
<hr>
</div>
</div>
这是我的 node.js 代码
router.get('/db-comments', function(req, res, next){
Comment.find(function(err, data){
if(err){
console.log('can not fetch the data')
}
res.send(data);
})
});
提前致谢!
【问题讨论】:
标签: javascript angularjs node.js mongodb ng-repeat