Reputation: 67
i have a simple service inside a controller:
function OPctrl($scope, $http) {
var s = 'http://query.yahooapis.com/v1/public/yql?q=select * from etc..... &format=json&callback=JSON_CALLBACK';
$http.jsonp(s).success(function(data) {
$scope.titoli = data.query.results.ROWSET.ROW;
}).
error(function(data, status, headers, config) {
alert("error!")
});
}
The controller bind some data in a table. I need to recall and refresh data every time i need (i.e. with a refresh button). Can i work inside my controller ?How to ? Or i need a custom service? I am new to angular can anyone help?
Upvotes: 3
Views: 4001
Reputation: 35829
It is better to use a custom service. To return the data to your controller, you have three possible options:
To use your service you just inject it in your controller:
app.contoller('Ctrl', ['$scope', 'service', function($scope, service) {
...
}]);
All three possibilities can be seen in action is this jsFiddle.
Upvotes: 1