在动态templateUrl的指令中使用promise

ZeG*_*egg 5 javascript angularjs angularjs-directive angular-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
}
});
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

ZeG*_*egg 3

我使用$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
            });
        });
      }
    };
});
Run Code Online (Sandbox Code Playgroud)

这是一个笨蛋

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