加载角度指令模板异步 [英] Load Angular Directive Template Async

查看:64
本文介绍了加载角度指令模板异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to be able to load the directive's template from a promise. e.g.

template: templateRepo.get('myTemplate')

templateRepo.get返回一个承诺,即解析后将模板的内容包含在字符串中.

templateRepo.get returns a promise, that when resolved has the content of the template in a string.

有什么想法吗?

推荐答案

您可以在指令中加载html,然后将其应用于元素并进行编译.

You could load your html inside your directive apply it to your element and compile.

.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){

              });
        }
    }
});

这篇关于加载角度指令模板异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆