Sam Sam - 3 months ago 43
AngularJS Question

Load Angular Directive Template Async

I want to be able to load the directive's template from a

promise
. e.g.

template: templateRepo.get('myTemplate')


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

Any ideas?

Rob Rob
Answer

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

              });
        }
    }
});