尝试从角度

时间:2016-06-09 08:11:07

标签: javascript angularjs node.js mongodb

我正在尝试从角度保存mongoDB中的数据。 我的数据从angular页面变为node.js页面,我想将数据保存在mongoDB中。 我将要保存的数据发送到控制器。 和我的架构来自localhost:8080 / activities 感谢,

我的角度看起来像那样:

 <html ng-app="addActivityApp">
     <body ng-controller="activityController">
     <h1 class="text-center">Add Activity</h1>
            <form class="form-group" ng-submit="createActivity()">
              <label for="inputEmail3" class="col-sm-2 control-label">Title:</label>
                <input class="form-control" type="text" placeholder="Title" ng-model="title"></input>
                <label for="inputEmail3" class="col-sm-2 control-label">Age:</label>
                <input class="form-control" id="inputEmail3" type="number" placeholder="Age" ng-model="age" min="0" max="16"></input>
                <label for="inputEmail3" class="col-sm-2 control-label">Description:</label>
                 <textarea class="form-control" id="inputEmail3" type="text" placeholder="Description" ng-model="description"></textarea>
                 <br>
                 <label for="inputEmail3" class="col-sm-2 control-label">Themes:</label>
                <input type="submit" class="btn btn-default" name="send"></input>
            </form> 
        <script src="js/lib/angular/angular.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.js"></script>
        <script src="js/addController.js"></script>
     </body>
     </html>

我的控制器看起来像这样:

var addActivityApp = angular.module('addActivityApp',[]);

var model = {};

addActivityApp.controller('activityController',function($scope) {
    $scope.addActivityApp = model;
});

addActivityApp.run(function($http) {
    $http.get("http://localhost:3000/activities").success(function(data){
addActivityApp.createActivity= function($scope){
    var activity = new Activity();
    activity.title = $scope.title;
    activity.age = $scope.age;
    activity.description = $scope.description;
    activity.theme = $scope.theme;
    activity.$save(function (result) {
      $scope.activities.push(result);
      $scope.title = '';
      $scope.age = '';
      $scope.description = '';
      $scope.theme = '';
            });
        }
    });
});

我读到了它,我无法理解我做错了什么

本地主机:3000 /活动

var mongoose = require('mongoose');
var Activity = require('./activity');

exports.getData = function(req, res){
    Activity.find({},function(err, docs){
        console.log("docs "+docs);
        res.json(docs);
        return;
    });
}

1 个答案:

答案 0 :(得分:0)

不是100%确定angularjs,但是你可以在项目中包含一个nodejs模块,用于mongodb https://www.npmjs.com/package/mongodb

使用它就像:

var MongoClient = require('mongodb').MongoClient;
var assert      = require('assert');

/**
 * save array of json objects to mongodb
 * @param issues
 */
Mongo.saveToMongo = function (issues) {

    // Use connect method to connect to the Server
    MongoClient.connect(mongourl, function (err, db) {
        assert.equal(null, err);
        console.log("Connected successfully to Mongo");

        // Get the issues collection
        var collection = db.collection('issues');

        console.log("Inserting issues");

        // insert all issues retrieved
        collection.insertMany(issues, function (err, result) {
            assert.equal(err, null);
            console.log("Inserted new issues");
            db.close();
        });
    });
}

至少你可以用它来改造你自己的角度连接。