Marta Castro
Marta Castro

Reputation: 77

AngularJS - custom directive with attributes in templateUrl

I have some templates for format text, frecuencia-dia.html or frecuencia-mes.html for example.

I want call a dinamic template using the attributes tipo (text plain) and clave (variable scope).

<ng-formato-texto tipo="frecuencia" clave="{{prod.claveFrecuencia}}" />

app.directive('ngFormatoTexto', function() {
    return {
        templateUrl: function(elem, attr){
            return '/formats/'+attr.tipo+'-'+attr.clave+'.html';
        }
    };
});

But don't work, this try load frecuencia-%7B%7Bprod.clavePrueba%7D%7D.html

Upvotes: 1

Views: 1058

Answers (1)

Stepan Kasyanenko
Stepan Kasyanenko

Reputation: 3186

You can not use the dynamic template in directive.

As documentation say:

Note: You do not currently have the ability to access scope variables from the templateUrl function, since the template is requested before the scope is initialized.

To create a dynamic directive can use ng-include.

Example:

app.directive('ngFormatoTexto', function() {
 return {
     template: '<div ng-include="path"></div>',
     scope:{
       tipo:"@",
       clave:"="
     },
     link:function(scope){
       scope.path= '/formats/'+scope.tipo+'-'+scope.clave+'.html'
     }
 };
});

Upvotes: 2

Related Questions