我正在努力应对延期承诺。我有一个非常难看的字符串:
我,公司名称,SSQ ID,您所在的公司(Extraction / XTR / 8North)以及分配给您公司的第17层问题。
| Y132〜
| Y133〜
| Y134~
| Y138~
| Y139〜
| Y140〜
| Y141〜
| Y142〜
| Y143〜
我必须更换每次出现的" | Y000~"使用URL链接。这部分代码工作正常。问题在于,我无法弄清楚如何使用承诺等待函数(包括推迟多个承诺)等待函数完成后再继续。
我在我的" convertString"功能:
getAllClusterLinks(indices, returnString)
returnString = $scope.returnString;
这是convetString函数:
function convertClusterText(questions, field) {
angular.forEach(questions, function (value, key) {
if (value.vchTextBeforeQuestionCluster != null) {
var str = value.vchTextBeforeQuestionCluster;
var returnString = str.replaceAll('|B', '<b>');
returnString = returnString.replaceAll("|b", "</b>");
returnString = returnString.replaceAll("|+", "<br/>");
returnString = returnString.replaceAll("|L", "<");
returnString = returnString.replaceAll("|R", ">");
returnString = returnString.replaceAll("|T", "<table border='1'>");
returnString = returnString.replaceAll("|/T", "</table>");
returnString = returnString.replaceAll("|S", "<tr>");
returnString = returnString.replaceAll("|/S", "</tr>");
returnString = returnString.replaceAll("|C", "<td>");
returnString = returnString.replaceAll("|/C", "</td>");
returnString = returnString.replaceAll("|A", "'");
returnString = returnString.replaceAll("|Q", "&");
returnString = returnString.replaceAll("|P", ";");
returnString = returnString.replaceAll("|W", """);
returnString = returnString.replaceAll("|H", "<hr style='width: 100%;'>");
returnString = returnString.replaceAll("|U", "<span style='text-decoration:underline'>");
returnString = returnString.replaceAll("|x", "</span>");
returnString = returnString.replaceAll("|N", "<span style='color:black'>");
returnString = returnString.replaceAll("|D", "<span style='color:blue'>");
returnString = returnString.replaceAll("|E", "<span style='color:red'>");
returnString = returnString.replaceAll("|G", "<span style='color:gray'>");
if (returnString.indexOf("|Y") >= 0) {
var indices = [];
var linkCode;
indices = getIndicesOf("|Y", returnString, true);
if (indices.length > 1) {
getAllClusterLinks(indices, returnString)
.then(function () {
returnString = $scope.returnString;
})
value.vchTextBeforeQuestionCluster = returnString;
}
else {
linkCode = getLink(returnString);
contractorService.gethyperlink(linkCode)
.success(function (data) {
var vchUrl = data[0].vchUrl;
var docID = getDocumentID(vchUrl);
var vchLinkName = data[0].vchLinkName;
questions[key].document = docID;
var yay = '<a href="" ng-click="getDocument(cluster)">' + vchLinkName + '</a>';
var yCode = "|Y" + linkCode + "~";
returnString = returnString.replaceAll(yCode, yay);
value.vchTextBeforeQuestionCluster = returnString;
})
}
}
else {
value.vchTextBeforeQuestionCluster = returnString;
}
}
});
};
我需要&#34; getAllClusterLinks&#34;在执行下一行之前完成。以下是&#34; getAllClusterLinks&#34;的代码:
function getAllClusterLinks(indices, returnString) {
var promises = [];
var times = 0
var endIndex = 0;
angular.forEach(indices, function (value, key) {
endIndex = getEndIndicesOf("~", returnString, value);
linkCode = getMultiLinks(returnString, value, endIndex)
var promise = getClusterLinks(linkCode, returnString);
promises.push(promise);
})
return $q.all(promises);
}
function getClusterLinks(linkCode, returnString) {
var deferred = $q.defer();
$scope.returnString = returnString;
contractorService.gethyperlink(linkCode)
.success(function (data) {
var vchUrl = data[0].vchUrl;
var end = vchUrl.length;
var docID = vchUrl.substring(vchUrl.indexOf("=") + 1, end);
var vchLinkName = data[0].vchLinkName;
var yay = '<a href="" ng-click="getDocument(' + docID + ')">' + vchLinkName + '</a>';
var yCode = "|Y" + linkCode + "~";
$scope.returnString = $scope.returnString.replaceAll(yCode, yay);
})
return deferred.promise;
}
上面的代码按预期工作,但我需要在设置行returnString = $scope.returnString;
之前先完成。
试过这个,但它不起作用:
getAllClusterLinks(indices, returnString)
.then(function () {
returnString = $scope.returnString;
})
非常感谢任何帮助!
答案 0 :(得分:1)
$ q.all(promises)返回一个承诺。你应该可以使用then()。
getAllClusterLinks(indices, returnString).then(function() {
returnString = $scope.returnString;
});
[https://docs.angularjs.org/api/ng/service/ $ Q] [1]
编辑:您应该解决延迟对象
旁注:我认为success()已被弃用,你应该使用.then too
function getClusterLinks(linkCode, returnString) {
var deferred = $q.defer();
$scope.returnString = returnString;
contractorService.gethyperlink(linkCode)
.success(function (data) {
var vchUrl = data[0].vchUrl;
var end = vchUrl.length;
var docID = vchUrl.substring(vchUrl.indexOf("=") + 1, end);
var vchLinkName = data[0].vchLinkName;
var yay = '<a href="" ng-click="getDocument(' + docID + ')">' + vchLinkName + '</a>';
var yCode = "|Y" + linkCode + "~";
$scope.returnString = $scope.returnString.replaceAll(yCode, yay);
deferred.resolve(); // resolve here
})
return deferred.promise;
}
答案 1 :(得分:0)
尝试将后续行放在 then()回调中:
getAllClusterLinks(indices, returnString)
.then(function() {
returnString = $scope.returnString;
});
有关角度内承诺的详细信息,请参阅the documentation