Reputation: 5704
I am beginning to mess around with angular js.
I wrote this factory
mbg.factory('pollCall', function ($http, pollSettings) {
return function (callback) {
$http({
method: 'GET', url: pollSettings.getUrl()
}).success(function (response) {
callback(response);
}).error(function () {
callback(false);
});
};
});
as you can see from it, it returns function rather than "return {};" object. This example works but I am thinking if it's possible way of designing a factory and if it won't break my app later on?
Upvotes: 0
Views: 93
Reputation: 42669
You can return anything with a factory and returning a function is fine. But in this context returning function is not required. You should use just return the promise value returned on $http
invocation.
mbg.factory('pollCall', function ($http, pollSettings) {
return {
getData:function() {
return $http({method: 'GET', url: pollSettings.getUrl()});
}
};
}
In the controller just use the same success and error callback as you did it in service.
pollCall.getData().success(...).error(...);
Upvotes: 1