I am trying to handle restangular calls entirely from a service to keep the controller light, and also so I can manipulate the data further within the service later.
Struggling with promises, which I think is my issue. If I can avoid it, I dont want the service to return the promise, and then use .then()
in the controller.
If I make the restangular call directly from the controller it works fine.
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
Any ideas why my combination of service and promise wont return the data to scope?