【发布时间】:2015-07-26 19:47:16
【问题描述】:
我正在尝试使用不同的指令模板。我发现的所有解决方案都是关于在链接器函数中使用 $compile service 。
但是我们有 template 属性用于指令工厂,它可以是一个函数。为什么不使用这个选项?
所以:
return {
template: function(element, attr) {
if(attr.template){
return attr.template;
}
return defaultTemplate;
}
}
在 html 之后:
<my-directive template="button.template"></my-directive>
像魅力一样工作。
只缺少一件事:我不知道如何在内部获取范围变量button.template。现在只是像字符串一样获取'button.template'。还尝试使用括号 - 仅使用括号“{{button.template}}”的相同字符串。是否有可能或者我应该返回到带有编译服务的解决方案?
【问题讨论】:
-
我认为这不起作用,因为在评估
{{button.template}}之前调用了您的模板函数。我想这只是您的指令的简化示例,否则您可以使用ng-include。 -
是的,你是对的,它是简化版。但是你能告诉我更多关于你对 ng-include 的想法吗?你打算如何将范围变量中的模板放在那里?
标签: angularjs angularjs-directive