angular(mvc)指令的嵌套使用
阿新 • • 發佈:2017-09-26
dht ive spa ren ron 引入 htm html tail
關於指令嵌套的使用,取值問題。
原理類似於控制器中使用指令,父指令類似於控制器,子指令就類似於控制器中指令。通過傳值方式‘=’,我們直接可以在父指令中獲取數據
舉一個例子:
有個指令parentDirective
模板文件是:parentHtml
還有一個childDirective
myapp.directive("childDirective",[function(){
return{
template:‘....../childHtml‘,
link:function(){
ngModel:‘=‘
}
}
}]);
模板文件:childHtml
在parentDirective中引入childDirective
myapp.directive("parentDirective",
‘.../js/directive/childDirective‘
[function(){
return{
template:‘....../parentHtml.html‘,
link:function(s,el,attr){
s.$watch(‘giveData‘,function(n,o){
console.log(n);
});
}
}
}]);
parentHtml.html
<child-directive ng-model="giveData"></child-directive>
如果你想互相引用指令的控制器,你可以看看下面的文章鏈接
http://blog.csdn.net/zhoukun1008/article/details/51296692
angular(mvc)指令的嵌套使用