我想在html页面上使用Angular显示一些查询mongoose结果数据

时间:2014-07-16 11:54:56

标签: node.js angularjs mongoose

我有一点问题,我想在html页面上使用Angular来显示一些mongoose查询结果数据。

我不知道如何将数据发送到角度$scope

app.js:

User.find({ Code: doc.Code}, function (err, data){
    var users = [];
    var userswithsamecode = '';
    data.forEach(function(d){
        console.log(d.nom + " " + d.prenom);
        users.push(d);
        userswithsamecode += "<li>" + d.nom +" "+ d.prenom + '</li><br>';

如何将此结果发送到角度$ scope,
collegues.html

<html data-ng-app="demo">
<div data-ng-controller="simple">
Name:
<br/>
<input type="text" data-ng-model="name" />
<br/>
<ul>
<li data-ng-repeat="collegue in collegues |filter:name">{{}} </li>  
</ul>
</div>
<script src="angular.min.js"></script>
<script>
var demo=angular.module('demo', []);
function simple($scope){
$scope.collegues=
     ];}
demo.controller('simple', simple);
</script>'

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

首先,您需要为mongo创建一个模型。一个例子

 var Example = mongoose.model('ExampleCollection'{
  name:String,
  description: String
  });

//posting to a mongodb
var example = new Example (
  app.post("/add-something-to-mongo", function(req, res) {
  name: req.body.name,
  description: req.body.description
  });
listing.save(function(err) {
     if(err) {
     console.log("Error! ", err);
     }else
      {
       console.log("Saved!"
      };
       });
     });
   });

角度代码看起来像这样。

var app = this;
var url = "heroku_url or localhost url";
app.save = function(Example) {
// create a new object
var newExample = {
"name": $scope.name
"description": $scope.description
};
$http.post(url + "/routefromserver", newExample).success(function() {
   console.log("posting");
})
};

最后是HTML

<input id="name" type="text" ng-model="name" />
<input id="city" type="text" ng-model="description"/>
<button ng-click="app.save()">Save Me</button>