0

Let's say I have a few directives: "drama", "comedy" and for some reason they have a lot of different properties, so it does not necessarily make sense to have a "movie" directive. Is there a way to dynamically evaluate a directive based on a scope variable? Something like this:

<{{movieType}} movie="{{movie}}"></{{movieType}}>

Where it would evaluate to something like this:

<comedy movie={{movie}}></comedy>

I'm new to Angular, so pardon for crazy ideas.

UPDATE: Actually just found a neat article regarding the exact same problem/solution: http://onehungrymind.com/angularjs-dynamic-templates/

Basically author has a single directive, but swaps out the templates based on the request:

var getTemplate = function(contentType) {
    var template = '';

    switch(contentType) {
        case 'image':
            template = imageTemplate;
            break;
        case 'video':
            template = videoTemplate;
            break;
        case 'notes':
            template = noteTemplate;
            break;
    }

    return template;
}

return {template: getTemplate(type)};
4
  • 2
    You cannot do that, you would need to construct the element compile and add it. Commented Jan 14, 2015 at 22:01
  • So should I then just create a 'movie' directive and have ng-switch there for different types of movies and nested directives for each category of movie? Commented Jan 14, 2015 at 22:08
  • What are the different options that can be passed in? They're all still movies, right? I would still explore the possibility of having a single movie directive and pass in all options, then you can ng-if or ng-show certain sections based on whether the input options are there. My guess is that they'll be similar enough that maintaining separate directives will be a maintenance nightmare. Commented Jan 14, 2015 at 22:40
  • OP, you can post an answer and accept it, if you like. Better to wait a few days, though, for others who might have other solutions. That is better than adding an answer to your Question, as you did. Commented Jan 14, 2015 at 22:57

1 Answer 1

1

You could use a template function inside of a directive:

 app.directive('movie', function(){
      return {
           restrict:'A',
           template: function(element, attr){
                 ... define templates, i.e. <comedy />
                 var contentType = attr.movie;
                 switch(contentType) {
                       case 'comedy':
                             template = comedyTemplate;
                             break;
                       case 'drama':
                             template = dramaTemplate;
                             break;
                       case 'suspense':
                              template = suspenseTemplate;
                             break;
                    }
                return template;
           }
      }
 })

With this solution, no manual compilation is necessary.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.