Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 带多个$HTTP Get请求的单角度控制器_Javascript_Angularjs_Node.js_Routing_Mongoose Populate - Fatal编程技术网

Javascript 带多个$HTTP Get请求的单角度控制器

Javascript 带多个$HTTP Get请求的单角度控制器,javascript,angularjs,node.js,routing,mongoose-populate,Javascript,Angularjs,Node.js,Routing,Mongoose Populate,我的服务器端运行了两个mongoose模式。我想在我的app.js中添加两个$http.get请求,并最终在网页上显示我在MongoDB中收集的两个表。只调用了一个get函数,没有错误 server.js //Data Schema var tempSchema = new mongoose.Schema({ topic: String, message: Number, when: Date }, {collection: "temperature"}); var h

我的服务器端运行了两个mongoose模式。我想在我的app.js中添加两个
$http.get
请求,并最终在网页上显示我在MongoDB中收集的两个表。只调用了一个
get
函数,没有错误

server.js

//Data Schema
var tempSchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "temperature"});

var humiditySchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "humidity"});

var temperature =mongoose.model('temperature', tempSchema);
var humidity =mongoose.model('humidity', humiditySchema);
app.controller("FormController", function ($http, $scope){
    $http.get("/api/temperature")
        .then(function (response) {
            $scope.temperatures = response.data;
        });
})

app.controller("FormController", function ($http, $scope){
    $http.get("/api/humidity")
        .then(function (response) {
            $scope.humiditys = response.data;
        });
})
app.js

//Data Schema
var tempSchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "temperature"});

var humiditySchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "humidity"});

var temperature =mongoose.model('temperature', tempSchema);
var humidity =mongoose.model('humidity', humiditySchema);
app.controller("FormController", function ($http, $scope){
    $http.get("/api/temperature")
        .then(function (response) {
            $scope.temperatures = response.data;
        });
})

app.controller("FormController", function ($http, $scope){
    $http.get("/api/humidity")
        .then(function (response) {
            $scope.humiditys = response.data;
        });
})
我还想知道如何在网页上显示这两个集合。使用
ng重复
。不幸的是,我不能在这里粘贴我的HTML代码


如果能得到任何帮助,我将不胜感激。谢谢

处理$http请求的另一种方法是创建角度工厂

angular.module('myApp.services',[])

然后在控制器内部,您应该执行以下操作(注意,必须将工厂作为控制器的依赖项注入)

然后定义angular应用程序(大多数情况下为App.js):


您已经定义了两次FormController。索引是否与应该一起使用的数据匹配?所以
temperatures[0]
将与
humiditys[0]
?@jbrown我应该删除其中的一个吗?您应该将ajax调用放在服务中,而不是controller@ProfessorAllman对不起,先生,我对这个平台还比较陌生,请你说得更清楚一点。我是否将angular factory创建为一个单独的JS文件?功能是一样的。但就最佳实践而言,最好创建一个新文件。好的,我刚刚添加了一个单独的JS文件,并添加了代码的第一部分。还有什么我需要补充的吗?我只是想确定我的回答是否正确。再次检查,我更新了答案,只是为了更清楚。非常感谢你的帮助。但我必须说,我觉得我有点困惑,我应该把文件放在上面。我希望能得到一些指导。
angular.module('myApp', ['myApp.controllers','myApp.services'])
    .run(...)
    .config(...)
    ...