Reputation: 47
I am using a service inside an angular controller like so: my Service:
MyServices.factory('Product', ['$http',function($http){
this.fn = function(city, state, category, categoryDetailValue) {
return $http.get('serverURL',{params: {city:city, state:state, category:category, categoryDetailValue:categoryDetailValue}}
).then(function(results){
return results.data;
});
};
}]);
my Controller:
$scope.init = function () {
console.log(Product);
Product.fn($rootScope.city, $rootScope.name, 'param1', 'param2').then(function(results) {
// Do something with results
$scope.items = results.Transactions;
});
};
when i try to call the Service inside the controller, the error says "Product is undefined"; please correct me where is my mistake?
Upvotes: 1
Views: 1465
Reputation: 35885
Use MyServices.service('Product',...
instead of MyServices.factory('Product',
I think it is because a factory it is supposed to be a function that returns and object, and it is not called using new
, so this
is not what you think it is.
If you wanted to use a factory, it would be something like this:
MyServices.factory('Product', ['$http',function($http){
return {
fn : function(city, state, category, categoryDetailValue) {
return $http.get('serverURL', {
params: {
city:city,
state:state,
category:category,
categoryDetailValue:categoryDetailValue
}
}).then(function(results){
return results.data;
});
}
};
}]);
Upvotes: 1