在动态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
更多推荐
发布评论