如何使用参数angularjs进行间隔调用
如何使用参数$interval调用函数如何使用参数angularjs进行间隔调用,angularjs,angularjs-directive,angularjs-ng-repeat,Angularjs,Angularjs Directive,Angularjs Ng Repeat,如何使用参数$interval调用函数 $interval( getrecordeverytime(2), 100000); function getrecordeverytime(contactId) { console.log(contactId + 'timer running'); } 试试这个 function getrecordeverytime(contactId){ console.lo
$interval( getrecordeverytime(2), 100000);
function getrecordeverytime(contactId)
{
console.log(contactId + 'timer running');
}
试试这个
function getrecordeverytime(contactId){
console.log(contactId + 'timer running');
}
$interval(function(){getrecordeverytime(2)},100000);
您可以从
$interval
的第五个参数开始传递参数:
angular.module('app',[]).controller('ctrl',function($scope,$interval){
函数getrecordeverytime(contactId,秒){
log(`${contactId},${second}计时器正在运行`);
};
$interval(getrecordeverytime,1000,0,true,2,5);
})
或者,您可以创建一个返回间隔回调函数的函数,并通过闭包将参数绑定到回调函数。像这样:
function createRecordCallback(contactId){
return function(){
console.log(contactId + 'timer running'); // the value of contactId, will be bound to this function.
};
}
$interval(createRecordCallback(1234), 100000);
这只是作为一种选择。在大多数情况下,我推荐斯拉瓦的答案