一:指令中关于模板问题
exModule.directive('expander',function(){
return{
restrict:'AE',
require:'^?accordion',
transclude:true,
replace:true,
scope:{
title:'=expanderTitle'
},
template:'<div class="title" ng-click="toggle()">{{title}}</div><div class="body" ng-transclude ng-show="showme"></div>',
link:function(scope,element,attrs,accordincontroller){
scope.showme=false;
accordincontroller.addExpander(scope);
scope.toggle=function toggle(){
scope.showme=!scope.showme;
accordincontroller.getOpened(scope);
};
}
}
});
该代码中template 必须以一个div元素的形式作为模板,否则会报错:
Error: [$compile:tplrt] Template for directive 'expander' must have exactly one root element.
二.指令中关于require引用
可通过require引用其他指令或者控制器、服务来调用某些函数或者属性;
require:'^?accordion'
即为引用accordion指令,且其中^符号表示从其上级中向上搜索,?符号表示可有可无