简体   繁体   English

通过承诺加载角度1.5组件模板

[英]Load angular 1.5 component template via promise

In Angular 1.5, I want to load the template via custom promise. 在Angular 1.5中,我想通过自定义承诺加载模板。 The example code that I would like to run is 我想要运行的示例代码是

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 () {

}
});

The reason I want to do this is to load the template from a proxy iframe. 我想这样做的原因是从代理iframe加载模板。

If there is any way to provide my custom template resolver for promise that would too suffice. 如果有任何方法可以为我的自定义模板解析器提供足够的功能。

I solved the problem by replacing $templateRequestService of angular using decorator. 我通过使用装饰器替换角度为$ templateRequestService来解决问题。

See the code example below: 请参阅下面的代码示例:

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;
    }]);

}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM