[英]Refactoring my angular code
I have a code here, where I used in there different controller. 我在这里有一个代码,在这里我在不同的控制器中使用过。 I thought of shifting this code to a factory but unable to do so.
我曾想过将此代码转移到工厂,但无法这样做。 I'll explain more clearly.
我会更清楚地解释。 Below is my code.
下面是我的代码。
scope.countDown = 4;
scope.runCounter = function() {
scope.countDown -= 1;
if ( scope.countDown > 0) timeout(scope.runCounter, 1000);
else scope.loginUser(); //can be different function
};
What it does is just a countdown timer. 它只是一个倒数计时器。 When the countdown hit to 0 it will call a scope function.
当倒数达到0时,它将调用范围函数。 As far as my re factoring go I have this:
就我的分解而言,我有这个:
my factory: 我的工厂:
runCounter : function(countDown, callback) {
rootScope.countDown = countDown;
var self = this;
rootScope.countDown -= 1;
if ( rootScope.countDown > 0) timeout(self.runCounter, 1000);
// else callback();
}
I dont know how to pass a function inside my factory function. 我不知道如何在工厂函数内部传递函数。 How can I achieve this?
我该如何实现?
Thanks, 谢谢,
I am not sure what you want to achieve, but this is a standard use case for $timeout
service. 我不确定您要实现什么,但这是
$timeout
服务的标准用例。
$timeout(myfunction,noOfSeconds * 1000);
in your case 在你的情况下
$timeout(scope.loginUser,4 * 1000);
See documentation 参阅文件
Update You can do something like 更新您可以做类似的事情
runCounter : function(countDown, callback) {
$timeout(callback,countDown* 1000);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.