最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - success and error function in the controller for a service - Stack Overflow

programmeradmin1浏览0评论

I have the following code in a service and I am calling fetchData function from the controller.

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?

I have the following code in a service and I am calling fetchData function from the controller.

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?

Share Improve this question edited Jun 22, 2015 at 11:11 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked Jun 22, 2015 at 10:37 user2851669user2851669 1
  • Use angular's promises docs.angularjs/api/ng/service/$q – devqon Commented Jun 22, 2015 at 10:40
Add a ment  | 

2 Answers 2

Reset to default 3

..I want to write the success and error function of $http.get in the controller..

Usually, the .then() function takes two function arguments. The first argument is the success handler and the second as an error handler.

$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.

$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 .

   ... 
   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

Seems like you want to return a data after all the ajax gets pleted, You could achieve this by using $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 pleted
        });
    };
});
发布评论

评论列表(0)

  1. 暂无评论