如何运行使用$超时在AngularJS参数的功能? [英] How to run function with parameters using $timeout in AngularJS?

查看:86
本文介绍了如何运行使用$超时在AngularJS参数的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的AngularJS控制器中此​​功能。它看起来像这样;

I have this function inside my AngularJS controller. It looks like this;

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

它使用$超时服务AngularJS保持自称。这工作直到我想参数添加到这个话题的功能。我的code看起来像这样的参数增加;

It uses the $timeout service in AngularJS to keep calling itself. This works until I wanted to add parameters to this poll function. My code looks like this for the parameters added;

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

的语法是不能接受的,我茫然了。我如何执行与使用$超时在AngularJS参数的功能?如果不能做到,有没有变通办法解决这个问题?我想有我的poll函数接受参数。

The syntax was not acceptable and I am at a loss now. How do I execute the function with parameters using $timeout in AngularJS? If this cannot be done, are there work-arounds to this problem? I would like to have my poll function accept parameters.

非常感谢您的任何帮助。

Thank you very much for any help.

推荐答案

由于第一个参数类型的 $超时的是功能,你需要做的是这样的:

Because the first parameter type of the $timeout is function, you need to do like this:

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

这篇关于如何运行使用$超时在AngularJS参数的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆