2

I am trying to retrieve the "cart" in the following way Factory->Service->Controller. I am making an $http call but it is returning an object. If I debug I can see that the request is made and is retrieving the data (in the network section of the debugger).

angular.module('companyServices', [])
.factory('CompanyFactory', ['$http', function($http){
    return {
        getCart: function(cartId) {
            var promise = $http.get('company/Compare.json', {params: {'wsid': cartId}})
             success(function(data, status, headers, config) {
                return data;
            }).
            error(function(data, status, headers, config) {
                return "error: " + status;
            });
        }
    };
}]);

angular.module('itemsServices', [])
.service('ItemsServices', ['CompanyFactory', function(CompanyFactory){
    var cartId = new Object();
    this.getCartId = function(){
        return cartId;
    };
    this.cartId = function(value){
        cartId = value;
    };
    this.getCart = function(){
      return CompanyFactory.getCart(this.getCartId()).then(function(data){return data});
    };
};

.controller('CompareItemsCtrl', ['$scope', '$location', 'ItemsServices', function($scope, $location, ItemsServices){
  var params = $location.search().wsid;
  ItemsServices.cartId(params);
  console.log('ItemsServices.getCart()');
  console.log(ItemsServices.getCart());
};

Thank you

1
  • 1
    so what's the problem then? You get a response, what's wrong with it?fwiw, I think you would want to pass in success and error handlers to getCart. getCart().then(successFunction, errorFunction) Commented Mar 25, 2014 at 3:33

1 Answer 1

2

Since $http returns a promise, I think you would be better of passing in your success and error functions to getCart()

.controller('CompareItemsCtrl', ['$scope', '$location', 'ItemsServices', function($scope, $location, ItemsServices){
  var params = $location.search().wsid;
  ItemsServices.cartId(params);
  console.log('ItemsServices.getCart()');
  console.log(ItemsServices.getCart());
  ItemsService.getCart().then(function(response){
    console.log('success');
  },function(response){
    console.log('error');
  });
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.