2016-06-23 5 views
5

1.5の角度で、私はカスタムプロミスを介してテンプレートをロードします。私が実行したい 例のコードは、私がこれをしたい理由は、プロキシはiframeからテンプレートをロードすることです約1.5度のコンポーネントテンプレートのロード

var module = angular.module("myApp", []); 
module.component("component", { 
template: ["$q", function ($q) { 

    var defer = $q.defer(); 

    setTimeout(function() { 
     defer.resolve("<p>Hello world</p>"); 
    }, 100) 
    return defer.promise; 
}], 
controller: function() { 

} 
}); 

です。

私のカスタムテンプレートリゾルバを約束するには余裕がある方法があれば。

+0

をhttp://stackoverflow.com/questions/22189298/angularjs-returning-a-promise-in-directive-template-functionこの記事を見て。これは、ディレクティブと同じ問題であるようです。私はあなたが同様のapprochを試すことができると思う – Silvinus

答えて

3

デコレータを使用してangleの$ templateRequestServiceを置き換えることで問題を解決しました。

は、次のコード例を参照してください。

module.config(["$provide", function ($provide) { 

$provide.decorator("$templateRequest", [ 
    "$delegate", "$q", // DI specifications 
    function ($delegate, $q) { 

     // replace the delegate function 
     $delegate = function (tpl) { 
      var defer = $q.defer(); 
      // convert the tpl from trustedvaluetoken to string 
      if (typeof (tpl) !== "string" || !!$templateCache.get(tpl)) { 
       tpl = $sce.getTrustedResourceUrl(tpl); 
      } 
      // make proxy call and resolve the promise; 

      // Make an async call 
      return defer.promise; 
     } 
     // return the modified delegate function 
     return $delegate; 
    }]); 

}]); 
+0

リターン$ q.when()を使用すると、直接遅延を避けることができます。 – Vitalii

関連する問題