2016-06-23 11 views
5

के माध्यम से कोणीय 1.5 घटक टेम्पलेट लोड करें कोणीय 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() { 

} 
}); 

कारण मैं इस एक प्रॉक्सी iframe से टेम्पलेट लोड करने के लिए है क्या करना चाहते है।

यदि मेरे कस्टम टेम्पलेट रिज़ॉल्वर को वादे के लिए प्रदान करने का कोई तरीका है जो बहुत अधिक होगा।

+0

इस पोस्ट http://stackoverflow.com/questions/22189298/angularjs-returning-a-promise-in-directive-template-function देखो:

नीचे कोड उदाहरण देखें। यह निर्देश के साथ एक ही समस्या प्रतीत होता है। मुझे लगता है कि आप एक समान दृष्टिकोण – Silvinus

उत्तर

3

मैंने सजावट का उपयोग करके कोणीय के $ 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

संबंधित मुद्दे