I have a "please wait, in progress" page. It is implemented with Angular controller: controller sends requests to the server once every few seconds to check if long running task is finished. If task is finished it redirects user to the next page. There is also a service that "knows" how to check the status of the job. Simplified versions of controller and service:
controller('MyCtrl', ["$scope", "$timeout", "MyService",
function($scope, $timeout, MyService) {
$scope.checkStatus = function() {
var request = MyService.fetch_status();
request.success(function(statusData) {
if (statusData.in_progress) {
$timeout(function() {
$scope.checkStatus();
}, 2000)
} else {
window.location = statusData.redirect_to;
}
});
};
$scope.checkStatus();
}]);
factory('MyService', ['$http', function($http){
return {
fetch_status : function() {
return $http({
url: '/job_status',
method: 'GET',
headers: { 'Accept': 'application/json' }
});
}
};
}]);
I want to reuse the same controller on a different page. This new page looks very similar, but it checks status of a different long running task and uses different URL. Is there a way to initialize the same controller with different service that "knows" how to check status of the task? Or is there a way to configure service to send request to a different URL? What is the AngularJS way to do this?