如何使用 angular 的 $timeout 的每个循环更新 $scope
How to update $scope with every loop of a $timeout with angular
我有一个 运行 在后台运行的功能,需要很长时间,我想在进行过程中向我网页的用户更新进度。我有一些 angular 询问结果/[jobID] 页面的进度是什么,并且成功 returns 进度。
它每 2 秒 运行s 并且 $log.log(data,status) 有效,所以我在日志中看到“10%”、“30%”等,但是我的 { {progress}} 页面上的变量仅在作业完成时更新(即触发 else if (status === 200) 循环)。
如何让 $scope.progress 在 poller() 的每个 运行 上更新 {{progress}}?
function getProgress(jobID) {
var timeout = '';
var poller = function() {
$http.get('/results/'+jobID).
success(function(data,status,headers,config) {
if(status === 202) { // This logs, but doesn't change {{progress}}
$log.log(data, status);
$scope.progress = data;
} else if (status === 200) { // This logs and changes {{progress}}
$log.log(data);
$scope.progress = data;
$timeout.cancel(timeout);
return false;
}
// continue to call the poller() function every 2 seconds
// until the timeout is cancelled
timeout = $timeout(poller,2000);
}).
error(function(error) {
$log.log(error);
});
};
poller();
}
getProgress(1234) //is called by another function on form submit
如果相关,我正在使用 flask 和 python 代码来制作实际页面
感谢 Faustyn Piechowiak,在这里提供有效答案:
function getProgress(jobID) {
var timeout = '';
var poller = function() {
$http.get('/results/'+jobID).
success(function(data,status,headers,config) {
if(status === 202) { // This logs, but doesn't change {{progress}}
$log.log(data, status);
$scope.progress = data;
$scope.comment = data;
try { // This updates the scope
$scope.apply;
}
catch(err) { // This also runs
$log.log("didn't apply")
}
} else if (status === 200) { // This logs, and changes {{progress}}
$log.log(data);
$scope.progress = data;
$timeout.cancel(timeout);
return false;
}
// continue to call the poller() function every 2 seconds
// until the timeout is cancelled
timeout = $timeout(poller,2000, true);
}).
error(function(error) {
$log.log(error);
});
};
poller();
}
getProgress(1234) //is called by another function on form submit
我有一个 运行 在后台运行的功能,需要很长时间,我想在进行过程中向我网页的用户更新进度。我有一些 angular 询问结果/[jobID] 页面的进度是什么,并且成功 returns 进度。
它每 2 秒 运行s 并且 $log.log(data,status) 有效,所以我在日志中看到“10%”、“30%”等,但是我的 { {progress}} 页面上的变量仅在作业完成时更新(即触发 else if (status === 200) 循环)。
如何让 $scope.progress 在 poller() 的每个 运行 上更新 {{progress}}?
function getProgress(jobID) {
var timeout = '';
var poller = function() {
$http.get('/results/'+jobID).
success(function(data,status,headers,config) {
if(status === 202) { // This logs, but doesn't change {{progress}}
$log.log(data, status);
$scope.progress = data;
} else if (status === 200) { // This logs and changes {{progress}}
$log.log(data);
$scope.progress = data;
$timeout.cancel(timeout);
return false;
}
// continue to call the poller() function every 2 seconds
// until the timeout is cancelled
timeout = $timeout(poller,2000);
}).
error(function(error) {
$log.log(error);
});
};
poller();
}
getProgress(1234) //is called by another function on form submit
如果相关,我正在使用 flask 和 python 代码来制作实际页面
感谢 Faustyn Piechowiak,在这里提供有效答案:
function getProgress(jobID) {
var timeout = '';
var poller = function() {
$http.get('/results/'+jobID).
success(function(data,status,headers,config) {
if(status === 202) { // This logs, but doesn't change {{progress}}
$log.log(data, status);
$scope.progress = data;
$scope.comment = data;
try { // This updates the scope
$scope.apply;
}
catch(err) { // This also runs
$log.log("didn't apply")
}
} else if (status === 200) { // This logs, and changes {{progress}}
$log.log(data);
$scope.progress = data;
$timeout.cancel(timeout);
return false;
}
// continue to call the poller() function every 2 seconds
// until the timeout is cancelled
timeout = $timeout(poller,2000, true);
}).
error(function(error) {
$log.log(error);
});
};
poller();
}
getProgress(1234) //is called by another function on form submit