我有4个功能,calculateSingle, calculateAll, process
和save
。
process
调用了一项服务,该服务保存了我重新计算的值,并且应始终在calculateAll
完成后进行。就我而言,这不会发生。 save
决定以某种方式甚至在所有calculateSingle
调用之前运行约4毫秒。
这是我的方法。我将save
函数作为对calculateAll
函数的回调。请考虑我编写的伪代码。
$scope.calculateSingle = function() {
//call web service to calculate single row in a table
}
$scope.calculateAll = function(callback) {
var promises = [];
for(var i in numberOfRows) {
promises.push($scope.calculateSingle(i));
}
//combine promises to call calculateSingle as many times as there are rows
$q.all(promises).then(
function successCallback(response) {
//take the response and apply all the recalculated values onto view
}
).then(
//SHOULD BE CALLED AFTER CALCULATE IS FINISHED AND VALUES ARE REASSIGNED
//This is the $scope.save() function passes as a callback function
if(callback) {
callback();
}
);
}
$scope.process = function() {
$scope.calculateAll($scope.save());
}
$scope.save = function() {
//run the save
}
答案 0 :(得分:3)
{
"text": "Hello {0} World {1} This is {2} test text"
}
呼叫立即保存;如果要将其作为回调传递,则应使用$scope.save()
$scope.save