【问题标题】:unable to display the http get response from the factory in controller in Angularjs application无法在 Angularjs 应用程序的控制器中显示来自工厂的 http get 响应
【发布时间】:2015-10-18 02:13:34
【问题描述】:

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

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

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

    [
   {
      "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
      }
   }
]

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

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

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

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

【问题讨论】:

    标签: angularjs angularjs-scope angularjs-service


    【解决方案1】:

    $http 返回一个承诺。任何消耗这些数据的东西也需要像承诺一样处理它。

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

    另外,你工厂的then 函数目前没有做任何事情。您应该从中返回响应的数据。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-19
      • 2014-12-16
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-14
      相关资源
      最近更新 更多