博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AngularJS学习日记(三)指令
阅读量:6871 次
发布时间:2019-06-26

本文共 4517 字,大约阅读时间需要 15 分钟。

复制代码
.directive('hello',function ($templateCache) {        return{            restrict:'AEMC',//分别代表四种模式 attribute element comment class 前两种常用                        template:'
Hi everyone!!!
', replace:true } })复制代码

restrict代表的是四种模式: A:attribute 属性 E:element 元素 comment: 注释 class: css样式类

其中A、E是用的比较多的, 所以我看了很多人其实在restrict中设置的也是AE。

template的缓存机制: 可以将replace的网页保存在缓存中,方法如下:

.run(function ($templateCache) {            $templateCache.put('hello.html','
Hi everyone!!!
') }) .directive('hello',function ($templateCache) { return{ restrict:'AEMC',//分别代表四种模式 attribute element comment class 前两种常用 template:$templateCache.get('hello.html'), //template:'
Hi everyone!!!
', replace:true } })复制代码

指令的 执行机制:

加载:加载AngularJS,找到ng-app,确定边界。编译:遍历DOM,找到template,replace,transclude,compile,对Html中的内容进行替换。链接:执行link,在DOM中绑定数据和事件监听器。复制代码

directive中的controller和link的区别: controller一般是让让指令暴露出方法让外部调用。 link处理指令内部的事务。(绑定数据,绑定监听器。)

    
动感超人--力量
动感超人--力量+敏捷
动感超人--力量+敏捷+发光
复制代码
.directive('superman',function () {        return{            scope:{},            restrict:'AE',            controller:function ($scope) {                $scope.abilities = [];                this.addStrength = function () {                    $scope.abilities.push('strength');                };                this.addSpeed = function () {                    $scope.abilities.push('speed');                };                this.addLight = function () {                    $scope.abilities.push('Light');                };            },            link:function (scope,element,attrs) {                element.addClass('btn btn-primary');                element.bind('mouseenter',function () {                    console.log(scope.abilities);                })            }        }    })    .directive('strength',function () {        return{            require:'^superman',            link:function (scope,element,attrs,supermanCtrl) {                supermanCtrl.addStrength();            }        }    })    .directive('speed',function () {        return{            require:'^superman',            link:function (scope,element,attrs,supermanCtrl) {                supermanCtrl.addSpeed();            }        }    })    .directive('light',function () {        return{            require:'^superman',            link:function (scope,element,attrs,supermanCtrl) {                supermanCtrl.addLight();            }        }    })复制代码

在第一个directive中,绑定的是superman标签,里面的controller就是把添加字符串到数组的方法暴露出来,让其他的directive可以调用。其他的directive通过require继承superman这个directive,在link中就能加入第四个参数,这里我起名是Supermanctrl,调用Supermanctrl就能用里面的方法,往数组里面加字符串,在前面绑定了时间,鼠标移动到上面,就会输出这个数组。

scope的三种绑定策略: @:把当前属性作为字符串传递。你还可以绑定来自外层scope的值,在属性值中插入{

{}}即可。 =:与父scope中的属性进项双向绑定。 &传递一个来自父scope的函数,稍后调用。

@方式:复制代码
复制代码
.controller('MyCtrl',['$scope',function ($scope) {        $scope.ctrlFlavor = '百威';    }])    .directive('drink',function () {        return{            restrict:'AE',            scope:{                flavor:'@'            },            template:'
{
{flavor}}
' } })复制代码

=方式:

Ctrl:
Directive:
复制代码
.controller('MyCtrl2',['$scope',function ($scope) {        $scope.ctrlFlavor = '百威';    }])    .directive('drink2',function () {        return{            restrict:'AE',            scope:{                flavor:'=',            },            template:''        }    })复制代码

&方式:

复制代码
.controller('MyCtrl3',['$scope',function ($scope) {        $scope.sayHello = function (name) {            alert('hello'+name);        }    }])    .directive('greeting',function () {        return{            restrict:'AE',            scope:{                greet:'&'            },            template:'
'+ '
' } })复制代码

AG有60种以上的自带指令,具体的官网开发文档都有。

form指令: 可以进行嵌套。 拓展了自动校验,防止重复提交等功能。 对input元素的type进行了扩展,提供以下10中类型: text、number、url、email、radio、checkBox、hidden、button、submit、reset 为表单内置了4种css样式: ng-valid、ng-invalid、ng-pritine、ng-dirty 内置校验器:require、minlength,maxlength

    
复制代码

转载地址:http://jecfl.baihongyu.com/

你可能感兴趣的文章
定时自动执行SQL存储过程(图文详解)
查看>>
R语言的包管理功能
查看>>
工厂模式
查看>>
JupyterHub on Kubernetes--定制用户环境
查看>>
sed
查看>>
不以成败论战略
查看>>
我的友情链接
查看>>
一个开源系统的架构分析
查看>>
系统基本功能
查看>>
der pem cer crt key pfx等概念及区别
查看>>
我的友情链接
查看>>
flume-ng 1.5.0安装部署
查看>>
http长连接与短连接
查看>>
数据库中快速备份一个表的数据,或者只备份表结构
查看>>
vue-cli
查看>>
文件处理
查看>>
代码调试包Infragistics Windows Forms Test Automation发布v16.1|附下载
查看>>
我的友情链接
查看>>
Android中自定义样式与View的构造函数中的第三个参数defStyle的意义
查看>>
Eclipse中提高Android SDK Manager下载速度方法
查看>>