javascript 服务的控制器中的成功和错误功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30977806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
success and error function in the controller for a service
提问by Pankaj Parkar
I have the following code in a service and I am calling fetchData function from the controller.
我在服务中有以下代码,我正在从控制器调用 fetchData 函数。
Service
服务
app.service("geturl", function($http) {
urllist = [];
geturl.fetchData = function() {
var data = [];
for (i = 0; i < urllist.length; i++) {
(function(index) {
return $http.get(geturl.urllist[index], {
timeout: 8000
})
.then(function(response) {
data[index] = response.data;
});
}(i);
return data;
});
};
});
I want to write the success and error function of $http.get in the controller since it is needed in the UI, how can I go about it?
我想在控制器中写$http.get 的成功和错误函数,因为它在UI 中是需要的,我该怎么做?
回答by nalinc
..I want to write the success and error function of $http.get in the controller..
..我想在控制器中写$http.get的成功和错误函数..
Usually, the .then()
function takes two function arguments. The first argument is the success handler and the second as an error handler.
通常,该.then()
函数采用两个函数参数。第一个参数是成功处理程序,第二个参数是错误处理程序。
$http.get(url,options).then(function (response){
//success handler function
},function(error){
//error handler function
})
Alternatively, you can specify the .success
and .error
functions separately.
或者,您可以单独指定.success
和.error
函数。
$http.get(url,options).success(function (response){
//success handler function
}).error(function(error){
//error handler function
})
UPDATE:From your code, it seems that you intend to return something from your service geturl
and providing the callbacks there itself. This is not how it is supposed to be done. You should return a promise from your service .
更新:从您的代码来看,您似乎打算从您的服务中返回一些内容geturl
并在那里提供回调。这不是它应该如何完成的。你应该从你的服务中返回一个 promise。
...
app.service("geturl",function($http){
...
getData:function(){
return $http.get(url,options);
}
...
});
...
and handle the success/error callbacks in the module where you are consuming the service
并在您使用服务的模块中处理成功/错误回调
geturl.getData().success(function(){
//handle success
}).error(function(){
//handle error
})
In case you need to make multiple http requests, never ever use the for loop. Remember, everything is asynchronous and you are not guaranteed to get the response from one of the previous request before you make a new one. In such scenarios, you should use $q service. See @pankajparkar's answer for more details
如果您需要发出多个 http 请求,请永远不要使用 for 循环。请记住,一切都是异步的,您不能保证在创建新请求之前从前一个请求之一获得响应。在这种情况下,您应该使用$q service。有关更多详细信息,请参阅@pankajparkar 的回答
回答by Pankaj Parkar
Seems like you want to return a data after all the ajax gets completed, You could achieve this by using $q.all()
似乎您想在所有 ajax 完成后返回数据,您可以使用 $q.all()
Service
服务
app.service("geturl", function($http, $q) {
this.urllist = [];
this.fetchData = function() {
var data = [], promises = [];
for (i = 0; i < urllist.length; i++) {
(function(index) {
var promise = $http.get(geturl.urllist[index], {
timeout: 8000
})
.then(function(response) {
data[index] = response.data;
});
promises.push(promise); //creating promise array
}(i);
};
return $q.all(promises).then(function(resp){
return data; //returning data after all promises completed
});
};
});