我遇到了AngularJS和MongoDb的交互問題。 我在服務器端使用NodeJS和ExpressJS。我用Mongoose跟Mongo說話。 我剛剛完成了路線。AngularJS顯示MongoDB數據
index.js
app.get('/api/dashboard', function (req, res) {
Homepage
.find({}, 'title -_id createdBy allbids.bid endTime')
.exec(function (err, auctions) {
if(err)
res.send(err);
console.log(auctions);
res.json(auctions);
})
});
控制檯顯示我需要的所有領域。
然後我通過前端。我用角路線是這樣的:
var app = angular.module('auction', [ 'ngRoute','HomeCtrl','NewAuctionCtrl', 'FollowingAuctionsCtrl', 'MyAuctionsCtrl']);
app.config(function ($routeProvider, $locationProvider)
{
$routeProvider
.when('/', {
templateUrl: 'views/partials/dashboard.html',
controller: 'HomeController'
})
$locationProvider.html5Mode(true);
});
而且我的HomeController是這樣的:
angular.module('HomeCtrl', [])
.controller('HomeController',function ($scope, $http) {
$http.get('/api/dashboard').then(function(data) {
console.log(data);
$scope.auctions= data;
})
});
在dashboard.html
<div class="panel panel-default" ng-controller="HomeController">
<div class="panel-heading">All The Auctions</div>
<div class="panel-body">
<ul class="list-group">
<li class="list-group-item" ng-repeat="auction in HomeController.auctions">
{{auction}}
</li>
</ul>
</div>
它不工作。我只需要使用{{auction.title}}
而不是{{auction}}
??
更改爲「拍賣中的拍賣」。在使用$ scope時,您不需要引用控制器。 編輯:然後像你評論得到像{{auction.title}}標題。 – Mickers