unable to display the http get response from the factory in controller in Angularjs application

雨燕双飞 提交于 2020-01-06 01:34:46

问题


In my service I making http get request as shown below:

.factory('InvoicesGeneralService', function ($http) {
        return {
            getAgreementsByCourierId: function (courierId) {  
            console.log("Courier in Services" + courierId);  
            return $http.get('/api/agreements/byCourierId', {params: {courierId: courierId}}).then(function (response) {
                return response;
            });
          }
        };
    });

And in browser console I am seeing the following response :

    [
   {
      "id":3,
      "number":"AGR53786",
      "ediNumber":"EDI7365",
      "startDate":"2012-09-02",
      "endDate":"2018-07-01",
      "courier":{
         "id":2,
         "name":"FedEx",
         "url":"www.fedex.com",
         "isActive":true,
         "isParcel":true
      },
      "client":{
         "id":4,
         "code":"KJGTR",
         "name":"Hearty",
         "isActive":true,
         "engageDate":"2011-07-07",
         "isSendRemittance":true,
         "upsUserName":"Tkd",
         "upsPassword":"kuu",
         "isEligibleForTracking":true,
         "isEligibleForAuditing":true,
         "status":5
      }
   }
]

And in my controller I am assigning it to result List :

  $scope.resultList  =  InvoicesGeneralService.getAgreementsByCourierId(selCourierId);

But my resultList is always appearing as Empty. Can any one help me, why it is happening? When I am trying to display resultList as shown below, it always shows empty object, {}. It supposed to display the response json array from the service but it is showing empty object.

<pre class="code"> {{resultList | json}}</pre> 

回答1:


$http returns a promise. Anything consuming that data needs to handle it like a promise too.

InvoicesGeneralService.getAgreementsByCourierId(selCourierId).then(function(data) {
  $scope.resultList = data;
});

Also, your factory's then function is not doing anything at the moment. You should return the response's data from it.

return $http.get('/api/agreements/byCourierId', {params: {courierId: courierId}}).then(function (response) {
  return response.data;
});


来源:https://stackoverflow.com/questions/33189765/unable-to-display-the-http-get-response-from-the-factory-in-controller-in-angula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!