Hello: I'm making a long polling with angular js and everything works until I make a getResponse function the program involves a) a main (at the bottom) b) a constructor to a service c) "the famous" getResponse which is a function of the service d) A simple console log to process response e) A setPolling service which is the loop f) Set response which makes a response to the service g) A simple directive
Can you tell why is throwing this.getResponse is not a function when actually is... ? heres the code
var DbService = function($http, $window, $interval) {
this.http_ = $http;
this.interval_ = $interval;
this.window_ = $window;
this.db = null;
};
DbService.prototype.getResponse = function() {
this.http_({
method: 'get',
url: 'dependencies/request.php',
data: ""
}).then(function successCallback(response) {
this.setResponse(response);
}, function errorCallback(response) {
console.log("Err response");
});
};
DbService.processResponse = function(response) {
console.log(this.response_);
};
DbService.prototype.setPolling = function() {
this.interval_(function(){
console.log("Hi World!");
this.response_ = this.getResponse();
this.processResponse(this.response_);
}, 10000);
};
DbService.prototype.setResponse = function(response) {
this.response_ = response;
};
var informAction = function($scope, DbService) {
return {
restrict: 'C',
link: function($scope, elem, attr, ctrl) {
action = DbService.getResponse();
$scope.consmensaje += "Accion: " + action.data['action'] + "\n";
}
};
};
var MainController = function($http, $window, $interval, DbService) {
this.http_ = $http;
this.window_ = $window;
this.interval_ = $interval;
this.DbService_ = DbService;
console.log("Dentro de Controller");
this.DbService_.setPolling();
};
function main() {
console.log("Comenzando");
var app = angular.module('consola', []);
app.service('DbService', DbService);
app.controller(
'MainController',
['$scope', '$http', '$interval', 'DbService',
MainController]);
app.directive('informAction', ['$scope', 'DbService', informAction]);
}
main();