ng自定义指令和四种用法
来源:互联网 发布:maxwell软件 编辑:程序博客网 时间:2024/06/06 08:40
ng自定义指令只有一种方法,通过directive去定义声明,使用有四种方法,作为元素、属性、class类、注释。以返回一个对象的方式定义。需要注意是名称必须以驼峰式命名,使用时变横杠的方式,比如名称为:myTest, 使用: my-test。ng内置的指令也是这么用的。
自定义指令的常用的属性有这么一些:name priority terminal scope controller require restrict template replace transclude compile link等。
<body ng-controller="myContr">
<!--作为标签使用-->
<my-test></my-test>
<!--作为属性使用-->
<div my-test></div>
<!--作为class类使用-->
<div class="my-test"></div>
<!--作为注释使用-->
<!-- directive:my-test -->
<script>
//声明ng模块
var app=angular.module("myModule",['ng']);
//自定义指令,两个参数,第一个参数为指令的名称,第二参数为设置指令的function
app.directive("myTest",function(){
return{
template:'<h1>Hello world</h1>',
restrict:"EACM",//指定四种使用模式,E/element A/attribute C/class M/comment
replace:'true' //作为注释使用,由于注释本就不显示的特殊性,需要用replace替换
}
});
</script>
需要传参的自定义指令
<body ng-controller="myContr">
<!--作为属性使用-->
<div my-test test="Hello world"> </div>
<!--作为元素使用-->
<my-test test="Hello world"> </my-test>
<script>
var app=angular.module("myModule",['ng']);
app.directive("myTest",function(){
return{
template:'<p>{{test}}</p>', 绑定接收参数的变量test
scope:{ //scope属性声明一块作用域,用来存储变量接收传过来的参数值
test:'@' //@符号相当形参 test为变量
}
}
});
</script>
</body>
- ng自定义指令和四种用法
- ng-自定义指令
- ng自定义指令
- FreeMarker中的自定义指令和四种变量
- (四上)Angular指令,ng-bind指令,ng-init 指令,ng-click指令,ng-repeat 指令
- ng-mouseenter和ng-mouseleave指令
- AngularJS中ng-app、ng-model、ng-bind、ng-init、ng-repeat、自定义指令
- ng-if、ng-show和ng-hide指令的区别
- ng-repeate 和ng-show的用法
- ng-class 指令的使用和介绍
- angular指令和自定义指令
- AngularJS:如何使用自定义指令来取代ng-repeat
- (十四)ng-if与ng-show、ng-hide指令的区别和注意事项
- AngularJs-ng-app -ng-model-ng-bind指令讲解和使用
- angularjs自定义指令的用法
- Angularjs中ng-select和ng-options的用法详解
- angularjs中ng-bind和ng-model用法
- ng-show,ng-hide,ng-if指令
- 2017年7月7日,周结(二十),实训之路开始
- Rspamd_rule_Html.lua自己的理解
- 嵌入式初体验
- LeetCode 605 : Can Place Flowers
- DL语义分割总结
- ng自定义指令和四种用法
- html讲课笔记
- 嵌入式系统学习——S3C2451嵌入式简介
- 605. Can Place Flowers
- 一起来看MyBatis(一)
- ionic3新特性懒加载,lazyloading
- 同一个Maven项目移机出错解决办法
- ROS机器人操作系统的安装、配置与初级教程 8
- ROS机器人操作系统的安装、配置与初级教程 9