您好:我正在使用角度js进行长轮询,一切正常,直到我创建了一个getResponse函数 该计划涉及 a)主要(在底部) b)服务的构造函数 c)“着名的”getResponse,它是服务的一个功能 d)用于处理响应的简单控制台日志 e)作为循环的setPolling服务 f)设置响应,以响应服务 g)一个简单的指令
你能说出为什么抛出这个.getResponse不是一个函数 实际上是什么时候......?继承人的代码
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();