Restangular,返回服务范围内的承诺响应范围

时间:2016-10-05 12:34:06

标签: angularjs promise restangular angular-services

我试图完全从服务处理restangular调用以保持控制器的亮度,同时我也可以在服务中进一步操作数据。

挣扎着承诺,我认为这是我的问题。如果我可以避免它,我不希望服务返回承诺,然后在控制器中使用.then()

如果我直接从控制器进行restangular调用,它可以正常工作。

angular.module('vehicle', ['restangular'])
  .config(
    function(RestangularProvider) {
      RestangularProvider.setBaseUrl('https://jsonplaceholder.typicode.com/');

      RestangularProvider.setResponseExtractor(function(response, operation, what) {
        if (operation === 'getList' && !Array.isArray(response)) {
          return [response];
        }
        return response;
      });
    })
  .controller('VehicleController', function($scope, VehicleService, Restangular) {
    $scope.vehicles = VehicleService.getVehicles();

    Restangular.all('posts').getList().then(function(vehicle) {
      $scope.moreVehicles = vehicle.plain();
    });

  })
  .service('VehicleService', function(Restangular) {
    VehicleService = {};

    VehicleService.vehicles = [];

    VehicleService.getVehicles = function() {
      Restangular.all('posts').getList().then(function(vehicle) {
        VehicleService.vehicles = vehicle.plain();

        return VehicleService.vehicles;
      });
    }

    return VehicleService;
  });

https://plnkr.co/edit/q1cNw6gN12pKsiPaD1o0?p=preview

为什么我的服务和承诺组合不会将数据返回到范围内的任何想法?

0 个答案:

没有答案