簡體   English   中英

通過承諾加載角度1.5組件模板

[英]Load angular 1.5 component template via promise

在Angular 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加載模板。

如果有任何方法可以為我的自定義模板解析器提供足夠的功能。

我通過使用裝飾器替換角度為$ 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;
    }]);

}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM