无法在 Angularjs 应用程序的控制器中显示工厂的 http get 响应 [英] unable to display the http get response from the factory in controller in Angularjs application

查看:17
本文介绍了无法在 Angularjs 应用程序的控制器中显示工厂的 http get 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的服务中,我发出 http get 请求,如下所示:

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

在浏览器控制台中,我看到以下响应:

<代码> [{身份证":3,"number":"AGR53786","ediNumber":"EDI7365","开始日期":"2012-09-02","endDate":"2018-07-01",导游":{身份证":2,"name":"联邦快递","url":"www.fedex.com",isActive":真,isParcel":真},客户":{身份证":4,"code":"KJGTR","name":"热情",isActive":真,"engageDate":"2011-07-07","isSendRemittance":true,"upsUserName":"Tkd","upsPassword":"kuu","isEligibleForTracking":true,"isEligibleForAuditing":true,状态":5}}]

在我的控制器中,我将它分配给结果列表:

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

但是我的 resultList 总是显示为 Empty.任何人都可以帮助我,为什么会这样?当我尝试如下所示显示 resultList 时,它总是显示空对象 {}.它应该显示来自服务的响应 json 数组,但它显示的是空对象.

{{结果列表 |json}}</pre>

解决方案

$http 返回一个 promise.任何使用该数据的东西也需要像承诺一样处理它.

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

此外,您工厂的 then 函数目前没有执行任何操作.您应该从中返回响应的数据.

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

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> 

解决方案

$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;
});

这篇关于无法在 Angularjs 应用程序的控制器中显示工厂的 http get 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆