AngularJS支持用户自定义标签属性,无需使用DOM节点操作即可添加用户自定义内容。
前面提到的AngularJS的四个特性是:
1个MVC
2模块化
3指令
4双向数据绑定
将介绍以下内容:
1如何定制说明
2自定义指令的使用
3自定义命令的嵌入式使用
如何自定义说明:
Angular是一个基于模块的框架,因此您必须创建自己的模块:
var myApp module=angular . module(' myApp ',[]);
然后在此模块的基础上创建指令
myAppModule.directive('xingoo ',function(){ return { restrict : ' AECM ',template : ' div hello my directive/div ',repalce : true } });Xingoo是我们自定义标记的名称,后面是它的方法函数。
函数返回一个键值对组合,定义标签的使用方法、属性等。
然后看看它定义了什么:
1限制:有四种使用标签的方式,即AECM
2模板:定义标签的模板。内部是用于替换自定义标签的字符串
3更换:是否支持更换
4 transclude:是否支持嵌入
如何使用命令:
上面提到了四种使用标签的方式,即AECM。
属性属性:用作属性
辛戈/迪格
元素元素:用作标记元素
xingoo/xingoo
c类类:用作CSS样式
div class='xingoo'/div
m注释注释:用作注释(此方法不适用于1.2版下的亲测!)
!-directive : xingo-div/div
一般来说,建议用作属性和元素。
当您想要扩展现有html标记的属性时,请使用属性。
当您想要自定义标签时,请使用标签的形式。
要使用该方法,必须在definition指令的restrict中声明相应的字母。
命令的嵌入式使用:
因为其他标签可以嵌套在标签中,所以如果您想在自定义标签中嵌套其他元素标签,您需要:
1使用transclude属性并将其设置为true。
2,并使用ng-transclude属性定义内部嵌套位置。
代码如下:
myAppModule.directive('test ',function(){ return { restrict : ' AECM ',transclude:true,template:'divhaha!div ng-transclude/div wuwu!/div ' } });所有代码
!doctype html html ng-app=' myApp ' head meta http-equiv=' Content-Type ' Content=' text/html;charset=utf-8 '/script src=' http :http://apps . bdimg.com/libs/angular . js/1 . 2 . 16/angular . min . js '/script/head body xingoo/xingoo div xingoo/div class=' xingoo '/div!-directive : xingo-div/div HR xing oo 3333/xing oo HR test 4444/测试脚本类型=' text/JavaScript ' var myApp module=angular . module(' myApp ',[]);myAppModule.directive('xingoo ',function(){ return { restrict : ' AECM ',template : ' div hello my directive/div ',replace : true } });myAppModule.directive('test ',function(){ return { restrict : ' AECM ',transclude:true,template:'divhaha!div ng-transclude/div wuwu!/div ' } });/script/body/htmlrunning结果
以上是AngularJS自定义说明的数据整理,后续继续补充相关数据。感谢您对本网站的支持!