它可以读取范围为指令templateUrl?TemplateUrl指令:AngularJs
我想要做这样的事情:
mDirective.directive('directive', [function() {
return {
restrict: 'A',
scope: {
types :'=types'
},
templateUrl:'.mytemplate/'+scope.types+'.html'
它可以读取范围为指令templateUrl?TemplateUrl指令:AngularJs
我想要做这样的事情:
mDirective.directive('directive', [function() {
return {
restrict: 'A',
scope: {
types :'=types'
},
templateUrl:'.mytemplate/'+scope.types+'.html'
范围在该指令的templateUrl可用。在github上有一个功能请求:Either add scope to attributes that are passed to templateUrl function or preprocess attributes based on scope parameters。
这里有两个选项(第二个是更通用的):
属性:范围不可用。但原始属性是。因此,如果原始属性为你工作,例如,如果它只是一个静态的字符串是这样的:
<div directive types="test1"></div>
然后我们可以通过函数到templateUrl
。第二个参数是属性,这样你就可以建立一个模板URL与字符串像这样:
templateUrl: function(elem, attrs){ return ('mytemplate/'+attrs.types+'.html')},
但如果types
可能会改变,所以你更好的解决方案可能是这不起作用:
ngInclude您可以参考一个范围可变的ngInclude
源表达的内部。因此,而不是使用templateURL
我们使用template
然后让ngInclude
处理设置/更改模板:
template: '<div ng-include src="\'mytemplate/\'+types+\'.html\'"></div>',
你也可以手动编译并添加伪指令中的模板。但使用ngInclude
很容易,也可以启用动画。
demo plunker显示这两个选项,并与一对夫妇的按钮切换模板,看看ngInclude
开关。