2016-09-16 12 views
0

私は延期された約束に苦しんでいます。私は非常に醜い文字列を持っています:機能の延期延期が機能の実行を遅らせることはありません

私、会社名、SSQ ID、あなたが働いている会社(抽出/ XTR/8North)、そしてあなたの会社に割り当てられた層#17。

| Y132〜
| Y133〜

| Y134〜

| Y138〜
| Y139〜
| Y140〜

| Y141〜
| Y142〜
| Y143 〜

"| Y000〜"の各オカレンスを置き換える必要がありますi 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", "&#39;"); 
      returnString = returnString.replaceAll("|Q", "&amp;"); 
      returnString = returnString.replaceAll("|P", "&#59;"); 
      returnString = returnString.replaceAll("|W", "&#34;"); 
      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; 

      } 


     } 
    }); 
}; 

私は次の行を実行する前に完了するために、 "getAllClusterLinks" が必要。ここで「getAllClusterLinks」のためのコードは次のとおりです。

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

関数内にも '$ scope.returnString = returnString; 'が設定されています。これがどのように機能すべきかをより詳細に説明する。私は文字列が毎回返されることを望んでいると思うが、目的が何であるかはっきりしていない – charlietfl

答えて

1

の$ q.all(約束)を参照してください約束を返します。 then()を使用できるはずです。

getAllClusterLinks(indices, returnString).then(function() { 
    returnString = $scope.returnString; 
}); 

[https://docs.angularjs.org/api/ng/service/ $ Q]

EDIT [1]:あなたがあなたの繰延オブジェクトを解決する必要があります

追記:私は(成功を信じている)がすでに廃止されて、あなたも.then使用する必要があります

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; 

} 
+0

残念ながら、私はそれを試み、 "then"は決して打撃を受けない。 –

+0

遅延オブジェクトを解決していません。私の更新された回答を参照 – doge1ord

+0

ありがとう! deferred.resolve()を追加します。それをやった!感謝万円! –

0

はコールバック)(その後、に次の行を入れてみてください:角度内での約束についての詳細情報については

getAllClusterLinks(indices, returnString) 
    .then(function() { 
     returnString = $scope.returnString; 
    }); 

the documentation

関連する問題