[英]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.