templateUrl with a function in angular2

Viewed 2342

Well... in angular 1.x.y is

angular.module('myApp', []).directive('myDirective', function(){
    return {
       templateUrl : function(tElement, iAttrs){
           return 'http://' + iAttrs.myDirective // More...
       } 
    }
});

But.. In Angular2

@Component({
    selector: 'my-Directive',
    templateUrl: 'http://???' 
})
class HelloWorld {
}

Well, in the doc say only a String. As it is handled to be a function in angular2 ?

1 Answers

I had to implement something similar and my solution was the same as Thomas Gassmann's comment above, so I decided to share.

Currently (angular 4.4.5) @Component decorator only accepts a string, therefore the template is not dynamically compiled like on angularJS. However you can implement multiple components and switch the component dynamically. Example below:

https://stackblitz.com/edit/angular-dynamic-templateurl

Related