我有一个 promise 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
}
});
谢谢你帮助我!
最佳答案
我使用 $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
关于javascript - 在动态 templateUrl 指令中使用 promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30106775/