marcel
marcel

Reputation: 3272

referencing a variable as directive template content

Is it possible to set a string-variable as the content of a template? I'd like to select two different templates depending on the scope. Something like this:

define(['app'], function(app){
    app.directive('logstorelist', function(){
        var temp="";
        return{
            scope: true,
            restrict: 'A',
            link: function(s, e, a){
                if(a=="a")
                    temp = "<a>tempA</a>";
                else
                    temp = "<div>temp</div>";
            },
            replace: true,
            template: temp
        }
    })
});

Is anything like this possible?

Upvotes: 0

Views: 97

Answers (1)

Eugenio Cuevas
Eugenio Cuevas

Reputation: 11078

You could use only one template and use ng-switch to load content depending on your scope variable (if you don't mind the extra <span>):

define(['app'], function(app){
    app.directive('logstorelist', function(){
        var temp="";
        return{
            scope: true,
            restrict: 'A',
            link: function(s, e, a){
                s.temp = a;
            },
            replace: true,
            template: 
            ' <span ng-switch="temp">
                <a ng-switch-when="a">tempA</a>
                <div ng-switch-default>temp</div>
            </span>'
        }
    })
});

Upvotes: 1

Related Questions