directive-自定义指令的方法.md
02directive——Angularjs的创建指令方法restrict 值分类:
directive的templateUrl属性通常我们这样写,template,但是如果模板里面东西很多,我们就需要独立出来一个html文件, myApp.directive('hello',function(){
return{
restrict : 'AEMC',//- A 作为属性使用(默认),例如:`<div hello></div>`
template : '<div>Hello everyone,I am bangbang!</div>',replace : true
}
})
用templateUrl属性独立出html文件,注意:如果用longDirective命名,引用的时候用 myApp.directive('longDirective',function(){
return {
restrict : 'AEMC',//- E 作为元素名使用,例如:`<hello></hello>`
templateUrl : 'tpls/long.html',replace : true
}
})
$templateCache(缓存模板方法)??通过angular创建的模块,都有一个run方法,注射器在加载完成所有模块的时候,该方法使用一次。接受一个函数作为参数.该函数会被执行.
myApp.run(function($templateCache){
// $templateCache.put('tpls/cache.html','<div>hello everyone!!!!</div>')
$templateCache.put('cache.html','<p>我是缓存的模板</p>')
});
myApp.directive('cacheDirective',function($templateCache){
return{
restrict : 'AECM',template : $templateCache.get('cache.html'),replace : true
}
})
transclude属性的使用(让创建的标签里面的内容不被替换)下面代码让html页面中的 myApp.directive("noReplace",function(){
return {
restrict : 'AECM',template : "<div>Hello everyone,I am noReplace!</div><div ng-transclude></div>",transclude : true
}
})
compile与link??Angularjs的运行流程如下所示:
myApp.directive('cpl',template : '<div>Hello bangbang</div>',replace : true,compile : function(){ //用来对模板自身进行转换
},link : function(){ //用来操作DOM和绑定事件监听器
}
}
})
githu源码:02directive —— 自定义指令的方法 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |