繁体   English   中英

使用MEAN堆栈-查询MongoDB并将值作为变量传递

[英]Using MEAN Stack - Querying MongoDB and Passing Value as a Variable

我正在使用MEAN堆栈来创建Web应用程序。 我有我的server.js,console.js,一个名为“ contactlist”的MongoDB和一个包含树形图Javascript的index.html。 我想做的就是查询我的MongoDB以获取密钥,然后返回其值(或者实际上只是获取任何信息)。 然后,我想将该值另存为变量,并将其传递给index.html中包含的javascript。

这是我重构教程代码最接近的代码。 使用mongojs作为我的驱动程序,我在server.js中有它:

app.get('/contactlist', function (req, res) {
console.log("I received a GET request")

db.contactlist.find({email:"bob11@email.com"},function(err, docs) {
//looks at the contact list database for anything with the email of bob11@email.com
    console.log(docs);
    res.json(docs);
});
});

在我的控制器中:

var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
console.log("Hello World from controller");

var refresh = function() {
$http.get('/contactlist').success(function(response) {
    console.log("I got the data I requested");
    $scope.contactlist = response;
    $scope.contact = "";
});
};

下面ng-repeat函数的使用是原始应用程序使用的人工产物(显示所有联系人的列表)。 现在我只想要一个值,但是我不知道要使用什么功能。 这是我的index.html:

<div class="container" ng-controller="AppCtrl">
    <table class="table">
        <thead>
            <tr>
        </thead>
        <tbody>
            <tr ng-repeat="contact in contactlist">
                <td>{{contact.name}}</td>
                <!-- grabs the name from parameters set out in server.js -->
            </tr>
        </tbody>
    </table>
</div>

这会在网页上显示我过滤的电子邮件的名称(“ Bob”)(bob11@email.com)。 在这一点上,即使只是能够将该名称保存为变量并将其传递给javascript也将有所帮助。 Ng-repeat绝对不是我想要使用的东西,但是我是编程和MEAN堆栈的新手,所以有点卡住了。

谢谢。

我不确定您要问什么,但我会尽力帮助您。

如果您询问如何在客户端上编辑contact.name变量,则应该熟悉Angular的双向数据绑定 这意味着对控制器中$ scope所做的任何更改都会影响HTML中显示的数据,而对html中变量的任何更改都将应用于$ scope。

例如,如果要在页面上编辑contact.name ,则可以使用绑定到contact.name的输入文本字段作为其模型。 为此,我们需要ng-model在输入字段和我们要更改的值之间架起一座桥梁。 可能看起来像这样

  <ul>
    <li ng-repeat="contact in contactlist">
      Name: <input type="text" ng-model="contact.name"/>
    </li>
  </ul>

然后,我们可以向输入中添加ng-change属性,以对更改执行操作。 在这里查看工作演示

第一个解决方案:您可以在html页面和绑定机制中使用ng-init这样的东西

<div ng-init="param='value';">
    <div ng-controller="yourController" >
        <label>param: {{value}}</label>
    </div>
</div>

现在,无论您在html文件中使用什么参数,控制器函数都可以使用

function yourController($scope) {
        console.log($scope.param);
}

第二种解决方案:您可以提供一种在控制器中传递值的服务,如下所示

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

app.factory('valuepassing', function() {

    var myvaluepassingService = {};
         data =[];
    myvaluepassingService.addvalue = function(value) {
        data.push(value);
    };
    myvaluepassingService.removevalue = function(value) {
        var index = data.indexOf(value);
        data.splice(index, 1);
    };
    myvaluepassingService.data = function() {
        return data;
    };

    return myvaluepassingService;
});

function MyCtrl($scope, valuepassing) {
    $scope.newvalue = {};
    $scope.valuepassing = valuepassing;    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM