Angularjs 需要为 angular-poller 的每个 tick 改变一个参数
Angularjs need to vary a parameter for each tick of angular-poller
我正在关注 angular-poller 演示:
https://emmaguo.github.io/angular-poller/
我需要能够将参数传递给工厂以构建动态 url,以便使用 newTime arg 调用 'greet'。
如何修改 poller.get() 以传入 'newTime'?
poller1 = poller.get(greet, {action: 'jsonp_get', delay: 1000});
.factory('greet', function ($resource) {
return $resource('https://www.example.com?time=' + newTime,
{
},
{
jsonp_get: { method: 'JSONP' }
});
})
/-- 演示 --/
angular.module('myApp', ['ngResource', 'emguo.poller'])
.factory('greet1', function ($resource) {
return $resource('https://angularjs.org/greet.php',
{
callback: 'JSON_CALLBACK',
name: 'Emma'
},
{
jsonp_get: { method: 'JSONP' }
});
})
.factory('greet2', function ($resource) {
return $resource('https://angularjs.org/greet.php',
{
callback: 'JSON_CALLBACK',
name: 'You'
},
{
jsonp_get: { method: 'JSONP' }
});
})
.controller('myController', function ($scope, poller, greet1, greet2) {
var poller1, poller2;
/*-- Automatically start poller1 on page load --*/
poller1 = poller.get(greet1, {action: 'jsonp_get', delay: 1000});
poller1.promise.then(null, null, function (data) {
$scope.data1 = data;
});
/*-- Actions --*/
$scope.stop = function () {
poller1.stop();
};
$scope.restart = function () {
poller1 = poller.get(greet1);
};
$scope.faster = function () {
poller1 = poller.get(greet1, {delay: 300});
};
$scope.slower = function () {
poller1 = poller.get(greet1, {delay: 1500});
};
$scope.startPoller2 = function () {
poller2 = poller.get(greet2, {action: 'jsonp_get', delay: 1000});
poller2.promise.then(null, null, function (data) {
$scope.data2 = data;
});
};
$scope.stopBoth = function () {
poller.stopAll();
};
$scope.restartBoth = function () {
poller.restartAll();
};
});
没有使用 angular-poller,但似乎你应该能够通过 属性 argumentsArray
得到你想要的:
var poller1 = poller.get(greet, {
action: 'jsonp_get',
delay: 1000,
argumentsArray: [{
time: newTime
}]
});
数组 argumentsArray
,即时间,在检索轮询器时进行计算。
编辑: 澄清后,您似乎需要为轮询的每个滴答更改一个参数,而不是当您 get轮询者。您可以通过在请求拦截器中添加参数来解决该问题:
// define interceptor
app.factory('requestInterceptor', function() {
return {
'request': function(request) {
// if needed, wrap the following in a condition
request.params = request.params || {};
request.params.time = moment();
return request;
}
}
})
// register interceptor
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('requestInterceptor');
})
我正在关注 angular-poller 演示: https://emmaguo.github.io/angular-poller/
我需要能够将参数传递给工厂以构建动态 url,以便使用 newTime arg 调用 'greet'。
如何修改 poller.get() 以传入 'newTime'?
poller1 = poller.get(greet, {action: 'jsonp_get', delay: 1000});
.factory('greet', function ($resource) {
return $resource('https://www.example.com?time=' + newTime,
{
},
{
jsonp_get: { method: 'JSONP' }
});
})
/-- 演示 --/
angular.module('myApp', ['ngResource', 'emguo.poller'])
.factory('greet1', function ($resource) {
return $resource('https://angularjs.org/greet.php',
{
callback: 'JSON_CALLBACK',
name: 'Emma'
},
{
jsonp_get: { method: 'JSONP' }
});
})
.factory('greet2', function ($resource) {
return $resource('https://angularjs.org/greet.php',
{
callback: 'JSON_CALLBACK',
name: 'You'
},
{
jsonp_get: { method: 'JSONP' }
});
})
.controller('myController', function ($scope, poller, greet1, greet2) {
var poller1, poller2;
/*-- Automatically start poller1 on page load --*/
poller1 = poller.get(greet1, {action: 'jsonp_get', delay: 1000});
poller1.promise.then(null, null, function (data) {
$scope.data1 = data;
});
/*-- Actions --*/
$scope.stop = function () {
poller1.stop();
};
$scope.restart = function () {
poller1 = poller.get(greet1);
};
$scope.faster = function () {
poller1 = poller.get(greet1, {delay: 300});
};
$scope.slower = function () {
poller1 = poller.get(greet1, {delay: 1500});
};
$scope.startPoller2 = function () {
poller2 = poller.get(greet2, {action: 'jsonp_get', delay: 1000});
poller2.promise.then(null, null, function (data) {
$scope.data2 = data;
});
};
$scope.stopBoth = function () {
poller.stopAll();
};
$scope.restartBoth = function () {
poller.restartAll();
};
});
没有使用 angular-poller,但似乎你应该能够通过 属性 argumentsArray
得到你想要的:
var poller1 = poller.get(greet, {
action: 'jsonp_get',
delay: 1000,
argumentsArray: [{
time: newTime
}]
});
数组 argumentsArray
,即时间,在检索轮询器时进行计算。
编辑: 澄清后,您似乎需要为轮询的每个滴答更改一个参数,而不是当您 get轮询者。您可以通过在请求拦截器中添加参数来解决该问题:
// define interceptor
app.factory('requestInterceptor', function() {
return {
'request': function(request) {
// if needed, wrap the following in a condition
request.params = request.params || {};
request.params.time = moment();
return request;
}
}
})
// register interceptor
app.config(function ($httpProvider) {
$httpProvider.interceptors.push('requestInterceptor');
})