使用AngularJs和MongoDB /猫鼬 [英] Using AngularJs and MongoDB/Mongoose

查看:167
本文介绍了使用AngularJs和MongoDB /猫鼬的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想AngularJS使用MongoDB的猫鼬连接。我想传递给由控制器使用的模型,这样我就可以$范围的数据。我不知道如果我去有关设置角服务,如果是这样,你能指出我在正确的方向。谢谢。

概述:

型号:

  VAR猫鼬=要求('猫鼬');
变种DB = mongoose.createConnection('的MongoDB://本地主机:3000 /数据库');
VAR orderSchema =新mongoose.Schema({
    routeFrom:字符串,
    routeTo:字符串,
    离开:字符串
});
VAR订单= db.model(订单,orderSchema);
module.exports =秩序;

控制器:

 当然'需要'不起作用//但你的想法
功能OrderController($范围){
  返回$ scope.orders = Order.find({});
}


解决方案

您需要有一个中间步骤。从直接角度去蒙戈将无法正常工作了。如果你想有一个通用的REST接口蒙戈与您可以利用角捆绑的$ HTTP服务,看一看的REST服务上蒙戈的网站清单。

蒙戈REST服务

<一个href=\"http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-RESTInterfaces\">http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-RESTInterfaces

角$ HTTP服务:

http://docs.angularjs.org/api/ng.$http

有很多不同的选择这里,但是这很可能是起床去最简单的方法。

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({});
}

解决方案

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.

这篇关于使用AngularJs和MongoDB /猫鼬的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆