Javascript 通过promise加载angular 1.5组件模板
在Angular 1.5中,我希望通过自定义承诺加载模板。 我想运行的示例代码是Javascript 通过promise加载angular 1.5组件模板,javascript,angularjs,angularjs-1.5,Javascript,Angularjs,Angularjs 1.5,在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
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 () {
}
});
var module=angular.module(“myApp”,[]);
组件(“组件”{
模板:[“$q”,函数($q){
var defer=$q.defer();
setTimeout(函数(){
延迟。解决(“你好世界””;
}, 100)
回报、承诺;
}],
控制器:函数(){
}
});
我想这样做的原因是从代理iframe加载模板
如果有任何方法可以为promise提供我的自定义模板解析器,那就足够了。我通过使用decorator替换angular的$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;
}]);
}]);
看这篇文章。指令似乎也有同样的问题。我认为您可以尝试类似的方法,您可以使用return$q.when()避免直接使用defer。