加载角度指令模板异步

Sam*_*Sam 4 angularjs

我希望能够从a加载指令的模板promise.例如

template: templateRepo.get('myTemplate')
Run Code Online (Sandbox Code Playgroud)

templateRepo.get 返回一个promise,当解析时,它具有字符串中模板的内容.

有任何想法吗?

Rob*_*Rob 13

你可以在你的指令中加载你的html,将它应用到你的元素并编译.

.directive('myDirective', function ($compile) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            //Some arbitrary promise.
            fetchHtml()
             .then(function(result){
                 element.html(result);
                 $compile(element.contents())(scope); 
              }, function(error){

              });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)