Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
646 views
in Technique[技术] by (71.8m points)

express - Using AngularJs and MongoDB/Mongoose

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.

Overview:

Model:

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;

Controller:

// of course 'require' does not work but you get the idea
function OrderController($scope) {
  return $scope.orders = Order.find({});
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 Services

http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-RESTInterfaces

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...