在动态templateUrl的指令中使用promise(Use promise in directive for dynamic templateUrl)

编程入门 行业动态 更新时间:2024-10-25 04:14:58
在动态templateUrl的指令中使用promise(Use promise in directive for dynamic templateUrl)

我有一个承诺SharedData,它也返回一个变量服务.template。 值是mytemplate,我用它构建一个url,我将传递给templateUrl指令,但没有成功。

app.directive('getLayout', function(SharedData) { var buildUrl= ''; SharedData.then(function(service) { buildUrl = service.template + '/layouts/home.html'; console.log(buildUrl); // return mytemplate/layouts/home.html which is the URL I want to use as templateUrl }); return { restrict: 'A', link: function(scope, element, attrs) {...}, templateUrl: buildUrl } });

谢谢你的帮助!

I have a promise SharedData which return a variable service .template as well. The value is mytemplate with which I build an url that I ant to pass to templateUrl directive but without success.

app.directive('getLayout', function(SharedData) { var buildUrl= ''; SharedData.then(function(service) { buildUrl = service.template + '/layouts/home.html'; console.log(buildUrl); // return mytemplate/layouts/home.html which is the URL I want to use as templateUrl }); return { restrict: 'A', link: function(scope, element, attrs) {...}, templateUrl: buildUrl } });

Thanks for helping me!

最满意答案

我使用$ templateRequest解决了我的问题

app.directive('getLayout', function($templateRequest, SharedData) { return { restrict: 'A', link: function(scope, element, attrs) { SharedData.then(function(service) { myTemplate = $templateRequest(service.template + '/layouts/home.html'); Promise.resolve(myTemplate).then(function(value) { element.html(value); }, function(value) { // not called }); }); } }; });

这是一个Plunker

希望这会帮助一些人:)并感谢@Matthew Green

I resolve my issue using $templateRequest

app.directive('getLayout', function($templateRequest, SharedData) { return { restrict: 'A', link: function(scope, element, attrs) { SharedData.then(function(service) { myTemplate = $templateRequest(service.template + '/layouts/home.html'); Promise.resolve(myTemplate).then(function(value) { element.html(value); }, function(value) { // not called }); }); } }; });

Here is a Plunker

Hope this will help some people :) and thanks to @Matthew Green

更多推荐

本文发布于:2023-08-04 22:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423298.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   动态   templateUrl   dynamic   promise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!