简体   繁体   English

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

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

I'm using the MEAN stack to create a web app. 我正在使用MEAN堆栈来创建Web应用程序。 I have my server.js, console.js, a MongoDB called 'contactlist', and an index.html that contains Javascript for a tree diagram. 我有我的server.js,console.js,一个名为“ contactlist”的MongoDB和一个包含树形图Javascript的index.html。 What I would like to do is query my MongoDB for say, a key, and return with its value (or really just grab any information). 我想做的就是查询我的MongoDB以获取密钥,然后返回其值(或者实际上只是获取任何信息)。 I'd like to then save that value as a variable and pass it along to the javascript contained in my index.html. 然后,我想将该值另存为变量,并将其传递给index.html中包含的javascript。

This is the closest I've gotten reconstructing a tutorial's code. 这是我重构教程代码最接近的代码。 Using mongojs as my driver, I have this in my server.js: 使用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);
});
});

In my controller: 在我的控制器中:

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 = "";
});
};

The use of the ng-repeat function below is an artifact of the original application's use (display a list of all contacts). 下面ng-repeat函数的使用是原始应用程序使用的人工产物(显示所有联系人的列表)。 Now I just want one value, but I don't know what function to use. 现在我只想要一个值,但是我不知道要使用什么功能。 Here's my index.html: 这是我的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>

This displays the name ("Bob") of the email I filtered for (bob11@email.com) onto the webpage. 这会在网页上显示我过滤的电子邮件的名称(“ Bob”)(bob11@email.com)。 At this point, even just being able to save that name as a variable and pass it along to the javascript would help. 在这一点上,即使只是能够将该名称保存为变量并将其传递给javascript也将有所帮助。 Ng-repeat is definitely not what I want to use, but I'm new to programming and the MEAN stack so I'm kind of stuck. Ng-repeat绝对不是我想要使用的东西,但是我是编程和MEAN堆栈的新手,所以有点卡住了。

Thanks. 谢谢。

I'm not totally sure what you're asking, But I'll try to help out. 我不确定您要问什么,但我会尽力帮助您。

If you're asking how to edit the contact.name variable on the client, you should probably familiarize yourself with Angular's two way data binding . 如果您询问如何在客户端上编辑contact.name变量,则应该熟悉Angular的双向数据绑定 This means that any change to $scope in the controller will affect the data displayed in the HTML, and any change to a variable in the html will be applied to $scope. 这意味着对控制器中$ scope所做的任何更改都会影响HTML中显示的数据,而对html中变量的任何更改都将应用于$ scope。

For example, if you want to edit the contact.name on a page, you could use an input text field bound to contact.name as its model. 例如,如果要在页面上编辑contact.name ,则可以使用绑定到contact.name的输入文本字段作为其模型。 To do that, we'll need ng-model to make a bridge between the input field and the value we want to change. 为此,我们需要ng-model在输入字段和我们要更改的值之间架起一座桥梁。 that might look something like this 可能看起来像这样

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

We can then add an ng-change attribute to the input to perform an action on change. 然后,我们可以向输入中添加ng-change属性,以对更改执行操作。 See working demo here 在这里查看工作演示

First solution: you can use ng-init for in your html page and binding mechanism something like this 第一个解决方案:您可以在html页面和绑定机制中使用ng-init这样的东西

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

now whatever parameter you are using in html file is available to your controller function 现在,无论您在html文件中使用什么参数,控制器函数都可以使用

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

second solution: you can make a service for passing the value in controller something like this 第二种解决方案:您可以提供一种在控制器中传递值的服务,如下所示

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