我正在尝试构建一个任务处理类型的SPA。想法是有10个步骤,用户单击提交,然后将调用服务以开始进程,UI将等待。一旦获得响应,将应用样式以确保失败。无论如何,现在我正试图嘲笑这个行为,我似乎无法让我的Angular正确,特别是$ timeout。下面是我的代码,请原谅我试图通过它来理解Angular的简化示例。
;function(angular) {
'use strict'
angular.module("workApp", [] )
.controller ("TaskController", function ($routeParams, workService, $timeout, $scope) {
var tc = this;
// These will be used in the view to flip the CSS to the appropriate color for success or failure
tc.step_0_status = "ready";
tc.step_1_status = "ready";
tc.step_2_status = "ready";
// trying this out, by storing my functions in a array, b/c I will have other uses in the end for this array.
var func_array = [
function(){step_0},
function(){step_1},
function(){step_2}
]
// This is where I am misunderstanding $timeout I guess. I simply want the loop to sleep for 3 seconds, before the next function is called.
$scope.startTasks = function results() {
for(var x = 0; x < func_array.length; x++) {
**$timeout(func_array[x](),3000);**
}
}
var step_0 = function() {
tc.step_0_status = "running"
}
var step_1 = function() {
tc.step_0_status = "completed";
tc.step_1_status = "running";
}
var step_2 = function() {
tc.step_1_status = "completed";
tc.step_2_status = "failed";
}
}
})(window.angular);
答案 0 :(得分:1)
您可以使用$interval创建所需的睡眠方法:
$interval(function(){
// do wahetever you need here
console.log('running');
},3000, func_array.length);
确保在使用后正确销毁它