Javascript AngularJS:从模块中的函数获取$http响应
如何从模块中的函数获取$http in的响应 角度模块:Javascript AngularJS:从模块中的函数获取$http响应,javascript,angularjs,callback,asynccallback,Javascript,Angularjs,Callback,Asynccallback,如何从模块中的函数获取$http in的响应 角度模块: // module customServices var customServices = angular.module("customServices", []); // object for response httpResponse = {content:null}; // function sendRequest function sendRequest(param) { // inject $http var ini
// module customServices
var customServices = angular.module("customServices", []);
// object for response
httpResponse = {content:null};
// function sendRequest
function sendRequest(param)
{
// inject $http
var initInjector = angular.injector(['ng']);
var $http = initInjector.get('$http');
// set header
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http({
// config
method: 'POST',
url: 'response.php',
data: $.param(param),
// success
}).success(function (response, status, headers, config) {
httpResponse.content = response;
// error
}).error(function (response, status, headers, config) {
console.warn("error");
});
}
// factory - moduleService
customServices.factory("moduleService", function () {
return {
// function - members
members: function(param)
{
switch(param.fc)
{
// getAll
case 'getAll':
sendRequest({
service :'members',
fc : param.fc,
id : param.id
});
return httpResponse;
}
},
};
});
控制器:
myApp.controller('main', ['$scope', '$http', 'moduleService', function($scope, $http, moduleService){
$scope.handleClick = function () {
var ReturnValue = moduleService.members({
fc:'getAll',
id:'123',
});
console.log(ReturnValue);
};
}]);
对象在第一次单击时为空,第二次单击时其内容为$http响应
但我希望控制器知道$http响应何时可用
我尝试使用$broadcast和$on,但在函数“sendRequest”中使用$rootScope似乎是不可能的
为什么要定义httpResponse而不是仅仅从sendRequest函数返回一些东西
为什么要在angular之外定义函数,而不是将其作为服务或工厂
您应该创建一个包含sendRequest方法的服务,如下所示:
customServices.factory("yourNameHere", function($http, $q) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
return {
sendRequest: function sendRequest(param) {
return $http({
method: 'POST',
url: 'response.php',
data: $.param(param),
})
.then(function(response) {
return response;
})
.catch(function(response) {
console.warn("error");
return $q.reject(response);
});
}
};
});
moduleService.members({
fc:'getAll',
id:'123',
})
.then(function(result) {
console.log(result);
});
然后在另一个服务中:
customServices.factory("moduleService", function (yourNameHere) {
return {
// function - members
members: function(param)
{
switch(param.fc)
{
// getAll
case 'getAll':
return yourNameHere.sendRequest({
service :'members',
fc : param.fc,
id : param.id
});
}
},
};
});
现在,结果将是一个承诺,因此您可以像这样使用数据:
customServices.factory("yourNameHere", function($http, $q) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
return {
sendRequest: function sendRequest(param) {
return $http({
method: 'POST',
url: 'response.php',
data: $.param(param),
})
.then(function(response) {
return response;
})
.catch(function(response) {
console.warn("error");
return $q.reject(response);
});
}
};
});
moduleService.members({
fc:'getAll',
id:'123',
})
.then(function(result) {
console.log(result);
});
这就是我要找的!但现在不调用“members”函数。相反,它抛出错误“TypeError:无法读取未定义“”的属性“then”?!不使用switch语句,只需添加一个getAll方法并从那里返回。我知道你的意思,但我无法让它工作:(你能给我一个提示,如何在函数“members”中定义一个方法“getAll”吗?我如何调用它?moduleService.members.getAll()?好的,谢谢!我没有在members函数中使用getAll方法,而是将moduleService重命名为moduleServiceMembers,members函数现在是getAll函数。太好了!如果需要更多帮助,请告诉我!