6

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 ?

4
  • Use templateUrl: 'http://...' Commented Mar 18, 2016 at 4:54
  • Its kind of similar question as this question with no answer... Commented Mar 18, 2016 at 7:58
  • @pixelbits OP wanted to have, dynamic templateUrl based on value passed from parent component.. the similar question I asked yesterday but didn't find any answer.. could you looked at my previous comment too. Commented Mar 18, 2016 at 8:10
  • Component templates are compiled at runtime in Angular 2 by the Angular Compiler. Therefore you can't choose your template dynamically with a function. EDIT: You could solve this problem by creating a component, which is using the dynamic component loader to dynamically create a component and then display it within your parent component. Commented Sep 3, 2017 at 11:09

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

The idea, is the templetes in de server say errors/timeout.html, errors/input_error.html .... Etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.