Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在AngularJS中使用$timeout运行带参数的函数?_Javascript_Angularjs - Fatal编程技术网

Javascript 如何在AngularJS中使用$timeout运行带参数的函数?

Javascript 如何在AngularJS中使用$timeout运行带参数的函数?,javascript,angularjs,Javascript,Angularjs,我在AngularJS控制器中有这个函数。看起来是这样的, polling_interval=1000; var poll = function() { //Execution code $timeout(poll, polling_interval); }; poll(); 它使用AngularJS中的$timeout服务来继续调用自己。在我想向这个轮询函数添加参数之前,这种方法一直有效。对于添加的参数,我的代码如下所示 polling_interval=1000; var p

我在AngularJS控制器中有这个函数。看起来是这样的,

polling_interval=1000;
var poll = function() 
{
  //Execution code
  $timeout(poll, polling_interval); 
}; 
poll();
它使用AngularJS中的
$timeout
服务来继续调用自己。在我想向这个轮询函数添加参数之前,这种方法一直有效。对于添加的参数,我的代码如下所示

polling_interval=1000;
var poll = function(param1, param2) 
{
  //Execution code
  $timeout(poll(param1, param2), polling_interval); 
}; 
poll(param1, param2);

语法不可接受,我现在不知所措。如何使用AngularJS中的
$timeout
执行带参数的函数?如果无法做到这一点,是否有解决此问题的方法?我想让轮询函数接受参数。

使用匿名函数可能是最简单的方法

polling_interval=1000;
var poll = function(param1, param2) 
{
  //Execution code
  $timeout(function () { poll(param1, param2) }, polling_interval); 
}; 
poll(param1, param2);

由于
$timeout
的第一个参数类型是函数,因此需要执行以下操作:

polling_interval=1000;
var poll = function(param1, param2) 
{
  //Execution code
  $timeout(function() {poll(param1, param2)}, polling_interval); 
}; 
poll(param1, param2);

$timeout是Angular对window.setTimeout的包装。当然,就像setTimeout一样,它支持向超时的fn传递额外的参数

来自AngularJS API:

$timeout([fn], [delay], [invokeApply], [Pass]);
[fn](功能)是您的功能
[延迟](数字)以毫秒为单位的延迟
[invokeApply](布尔值)默认为true,如果为true,则fn在$apply内运行,如果为false,则跳过模型脏检查
[Pass]其他参数!这就是你想要的

您的代码应该是什么样子的:

polling_interval = 1000;
var poll = function(param1, param2){
    //Execution code
    $timeout(poll, polling_interval, true, param1, param2); 
}; 
poll(param1, param2);
这是将参数传递给超时fn的正确方法。我希望你觉得这有用

编辑:此功能于2015年1月22日(v1.4.1)添加,在此版本之前,正确的方法是:

polling_interval = 1000;
var poll = function(param1, param2){
    //Execution code
    $timeout(poll.bind(null, param1, param2), polling_interval); 
}; 
poll(param1, param2);

这对我有帮助。但是请注意,只要您没有任何函数参数,它也可以在以下情况下工作:“myFunc=$timeout(someFunc(),somemillizes);”一旦someFunc()有了一个参数,它就不再工作了,必须按照您的建议执行:“myFunc=$timeout(function(){someFunc(param)},somemillizes);”为什么选择匿名函数而不是bind()?为什么选择匿名函数而不是bind()?最佳解决方案!我想知道如何在1.4.1之前再次执行so+1。使用匿名函数的其他解决方案可能由于闭包而产生意外行为。使用
.bind()
方法,您知道为什么第一个参数必须为
null
?当我没有这样做,参数变得混乱时,这让我有点困惑。@Will
.bind()
的第一个参数是函数将绑定到的
这个
,即“上下文”。事实上,这就是
.bind()
的主要用法。使用附加参数时,不仅要绑定上下文,还要绑定传递给
.bind()
的尽可能多的参数,因此,当调用生成的绑定fn时,使用第一个参数设置的
this
调用它,使用剩余参数设置的参数调用它。或者其他人可以从1.4.0中更准确地说明,您可以使用
$timeout([fn]、[delay]、[invokeApply]、[Pass])。文档