TemplateUrl with function in angular2

Well ... in angular 1.xy

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 doc they only say String . Since it is being treated as a function in angular2?

+6
source share
1 answer

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

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

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

0
source

All Articles