我有一些用于格式化文本的模板,例如frecuencia-dia.html或frecuencia-mes.html。
我想使用属性tipo(纯文本)和clave(可变范围)来调用动态模板。
<ng-formato-texto tipo="frecuencia" clave="{{prod.claveFrecuencia}}" />
app.directive('ngFormatoTexto', function() {
return {
templateUrl: function(elem, attr){
return '/formats/'+attr.tipo+'-'+attr.clave+'.html';
}
};
});
但是不起作用,请尝试加载frecuencia-%7B%7Bprod.clavePrueba%7D%7D.html
您不能在指令中使用动态模板。
如文档所述:
注意:您当前无法从templateUrl函数访问范围变量,因为在初始化范围之前需要模板。
要创建动态指令,可以使用ng-include
。
例子:
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'
}
};
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句