带有多个 $HTTP Get 请求的单个 Angular 控制器
Single Angular Controller w/ multiple $HTTP Get request
我的服务器端有两个 mongoose 模式 运行。我想在我的 app.js 中添加两个 $http.get
请求,并最终在网页上的 MongoDB 中显示我的 collection 中的两个表.只有一个 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.js
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;
});
})
也在考虑如何在网页上显示两个 collection。使用 ng-repeat
。不幸的是,我无法在此处粘贴我的 HTML 代码。
如果能得到任何帮助,我将不胜感激。谢谢
另一种处理 $http 请求的方法是创建一个 Angular 工厂。
angular.module('myApp.services',[])
add.factory('ApiService', function($http) {
return {
getHumidity: function() {
return $http.get("/api/humidity");
},
getTemperature: function() {
return $http.get("/api/temperature");
}
}
})
然后在你的控制器中,你应该做以下事情(注意你必须注入工厂作为控制器的依赖项)
angular.module('myApp.controllers',[])
.controller("FormController", function (ApiService, $scope){
function getHumidity() {
var promise = ApiService.getHumidity();
promise.then(
function(response) {
$scope.humiditys = response.data;
},
function(errorPayload) {
console.log(errorPayload);
});
};
function getTemperature() {
var promise = ApiService.getTemperature();
promise.then(
function(response) {
$scope.temperatures = response.data;
},
function(errorPayload) {
console.log(errorPayload);
});
};
getHumidity();
getTemperature();
})
那么你在哪里定义你的 angular 应用程序(app.js 在大多数情况下):
angular.module('myApp', ['myApp.controllers','myApp.services'])
.run(...)
.config(...)
...
我的服务器端有两个 mongoose 模式 运行。我想在我的 app.js 中添加两个 $http.get
请求,并最终在网页上的 MongoDB 中显示我的 collection 中的两个表.只有一个 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.js
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;
});
})
也在考虑如何在网页上显示两个 collection。使用 ng-repeat
。不幸的是,我无法在此处粘贴我的 HTML 代码。
如果能得到任何帮助,我将不胜感激。谢谢
另一种处理 $http 请求的方法是创建一个 Angular 工厂。
angular.module('myApp.services',[])
add.factory('ApiService', function($http) {
return {
getHumidity: function() {
return $http.get("/api/humidity");
},
getTemperature: function() {
return $http.get("/api/temperature");
}
}
})
然后在你的控制器中,你应该做以下事情(注意你必须注入工厂作为控制器的依赖项)
angular.module('myApp.controllers',[])
.controller("FormController", function (ApiService, $scope){
function getHumidity() {
var promise = ApiService.getHumidity();
promise.then(
function(response) {
$scope.humiditys = response.data;
},
function(errorPayload) {
console.log(errorPayload);
});
};
function getTemperature() {
var promise = ApiService.getTemperature();
promise.then(
function(response) {
$scope.temperatures = response.data;
},
function(errorPayload) {
console.log(errorPayload);
});
};
getHumidity();
getTemperature();
})
那么你在哪里定义你的 angular 应用程序(app.js 在大多数情况下):
angular.module('myApp', ['myApp.controllers','myApp.services'])
.run(...)
.config(...)
...