使用 AngularJs 和 MongoDB/Mongoose [英] Using AngularJs and MongoDB/Mongoose
问题描述
我正在尝试使用 Mongoose 将 AngularJS 与 MongoDB 连接起来.我想传递控制器使用的模型,所以我可以 $scope 到数据.我不确定我是否必须着手设置 Angular 服务,如果是这样,你能指出我正确的方向吗?谢谢.
I am trying to connect AngularJS with MongoDB using Mongoose. I would like to pass the Models to be used by the Controllers, so I can $scope to the data. I am not sure if I have to go about setting up an Angular Service, if so, can you point me in the right direction. Thank You.
概述:
型号:
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost:3000/database');
var orderSchema = new mongoose.Schema({
routeFrom : String,
routeTo : String,
leaving: String
});
var Order = db.model('Order', orderSchema);
module.exports = Order;
控制器:
// of course 'require' does not work but you get the idea
function OrderController($scope) {
return $scope.orders = Order.find({});
}
推荐答案
您将需要一个临时步骤.直接从 Angular 到 Mongo 是行不通的.如果您想要一个通用的 REST 接口到 Mongo,您可以利用它来利用 Angular 的捆绑 $http 服务,请查看 Mongo 站点上的 REST 服务列表.
You'll need an interim step there. Going directly from Angular to Mongo will not work out. If you want a generic REST interface to Mongo with which you can utilize Angular's bundled $http services, take a look at the list of REST services on Mongo's site.
Mongo REST 服务
Mongo REST Services
http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-REST接口
Angular $http 服务:
Angular $http Service:
http://docs.angularjs.org/api/ng.$http
这里有很多不同的选择,但这可能是最简单的方法.
There's a lot of different options here, but this is likely the easiest way to get up and going.
这篇关于使用 AngularJs 和 MongoDB/Mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!