AngularJS 指令的交互详解及实例代码


Posted in Javascript onSeptember 14, 2016

背景介绍

这例子是视频中的例子,有一个动感超人,有三种能力,力量strength,速度speed,发光light。

这三种能力作为三种属性,定义动感超人作为一个标签,只要添加对应的属性就能拥有该能力。

为了便于结果的展示,为标签添加鼠标的响应事件,当鼠标移动到对应的标签上就会触发一个方法,打印出具备的能力。

程序分析

html部分的代码如下:       

<div>
      <superman>nothing!</superman>
      <superman strength >strength!</superman>
      <superman strength speed >strength speed!</superman>
      <superman strength speed light >strength speed light!</superman>
    </div>

下面看看如何实现,首先依然是创建一个模块:

var myAppModule = angular.module("myApp",[]);

在该模块的基础上,创建标签superman,与前面类似。

myAppModule.directive("superman",function(){
        return{
          scope:{},
          restrict:'AE',
          transclude:true,
          template:"<div><div ng-transclude></div></div>",
          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,attr){
            element.bind("mouseenter",function(){
              console.log(scope.abilities);
            });
          }
        }
      });

这里不同的是,在方法内部有一个controller属性,这个并不是ng-controller这种控制器,而是指令对外开放的一个接口,里面声明的方法,在外部可以作为公开的方法使用,其他的指令可以通过依赖,使用这些方法。

接下来再创建三个能力对应的指令

myAppModule.directive("strength",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addStrength();
          }
        }
      });
      myAppModule.directive("speed",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addSpeed();
          }
        }
      });
      myAppModule.directive("light",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addLight();
          }
        }
      });

三个指令的代码都差不多,其中require指定了依赖的指令。

link中多了一个参数supermanCtrl,这个参数猜想是superman中的controller,所以命名采用superman+Ctrl的方式。

【由于不懂内部原理,这里仅仅是猜想,但是实验证明,如果改变这个参数的名字,会报错。】

声明了这三个指令,就可以把这三个指令当做super的属性来使用,当注明该属性时,就会触发内部的link内的方法,调用superman中公开的方法。

  总结起来,指令的交互过程:

1 首先创建一个基本的指令,在controller属性后,添加对外公开的方法。

2 创建其他交互的指令,在require属性后,添加对应的指令依赖关系;在link中调用公开的方法

全部程序代码:

<!doctype html>
<html ng-app="myApp">
  <head>
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>
  </head>
  <body>

    <div>
      <superman>nothing!</superman>
      <superman strength >strength!</superman>
      <superman strength speed >strength speed!</superman>
      <superman strength speed light >strength speed light!</superman>
    </div>
    <script type="text/javascript">
      var myAppModule = angular.module("myApp",[]);

      myAppModule.directive("superman",function(){
        return{
          scope:{},
          restrict:'AE',
          transclude:true,
          template:"<div><div ng-transclude></div></div>",
          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,attr){
            element.bind("mouseenter",function(){
              console.log(scope.abilities);
            });
          }
        }
      });
      myAppModule.directive("strength",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addStrength();
          }
        }
      });
      myAppModule.directive("speed",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addSpeed();
          }
        }
      });
      myAppModule.directive("light",function(){
        return{
          require:'^superman',
          link:function(scope,element,attr,supermanCtrl){
            supermanCtrl.addLight();
          }
        }
      });
    </script>
  </body>
</html>

 

 运行结果:

AngularJS 指令的交互详解及实例代码

        以上就是对AngularJS 指令的交互的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

Javascript 相关文章推荐
IE和Firefox下event事件杂谈
Dec 18 Javascript
js实现缓冲运动效果的方法
Apr 10 Javascript
JS实用的动画弹出层效果实例
May 05 Javascript
Linux下为Node.js程序配置MySQL或Oracle数据库的方法
Mar 19 Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
Feb 15 Javascript
关于Angular2 + node接口调试的解决方案
May 28 Javascript
微信小程序WebSocket实现聊天对话功能
Jul 06 Javascript
js取小数点后两位四种方法
Jan 18 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
Feb 21 Javascript
java实现单链表增删改查的实例代码详解
Aug 30 Javascript
原生JavaScript实现弹幕组件的示例代码
Oct 12 Javascript
node.js通过url读取文件
Oct 16 Javascript
jQuery实现带遮罩层效果的blockUI弹出层示例【附demo源码下载】
Sep 14 #Javascript
什么是JavaScript注入攻击?
Sep 14 #Javascript
jQuery实现可拖拽的许愿墙效果【附demo源码下载】
Sep 14 #Javascript
再谈javascript注入 黑客必备!
Sep 14 #Javascript
AngularJS 表达式详解及实例代码
Sep 14 #Javascript
Knockout结合Bootstrap创建动态UI实现产品列表管理
Sep 14 #Javascript
js注入 黑客之路必备!
Sep 14 #Javascript
You might like
一个高ai的分页函数和一个url函数
2006/10/09 PHP
Ubuntu中启用php的mail()函数并解决发送邮件速度慢问题
2015/03/27 PHP
摘自织梦CMS的HTTP文件下载类
2015/08/08 PHP
PHP中的随机性 你觉得自己幸运吗?
2016/01/22 PHP
详解PHP归并排序的实现
2016/10/18 PHP
在Laravel中使用MongoDB的方法示例
2019/11/11 PHP
学习ExtJS accordion布局
2009/10/08 Javascript
jquery随意添加移除html的实现代码
2011/06/21 Javascript
ie与ff下的event事件使用介绍
2013/11/25 Javascript
JS cookie中文乱码解决方法
2014/01/28 Javascript
用jquery仿做发微博功能示例
2014/04/18 Javascript
Javascript中的getUTCDay()方法使用详解
2015/06/10 Javascript
jquery转盘抽奖功能实现
2015/11/13 Javascript
通过jquery实现页面的动画效果(实例代码)
2016/09/18 Javascript
vue中计算属性(computed)、methods和watched之间的区别
2017/07/27 Javascript
nodejs 图片预览和上传的示例代码
2017/09/30 NodeJs
mpvue中配置vuex并持久化到本地Storage图文教程解析
2018/03/15 Javascript
微信小程序实现手指触摸画板
2018/07/09 Javascript
详解微信小程序实现仿微信聊天界面(各种细节处理)
2019/02/17 Javascript
小程序实现左滑删除效果
2019/07/25 Javascript
vue中的 $slot 获取插槽的节点实例
2019/11/12 Javascript
[04:48]DOTA2上海特锦赛小组赛第三日 TOP10精彩集锦
2016/02/28 DOTA
Python 元类使用说明
2009/12/18 Python
Python获取apk文件URL地址实例
2013/11/01 Python
python使用chardet判断字符串编码的方法
2015/03/13 Python
python 列表递归求和、计数、求最大元素的实例
2018/11/28 Python
python3实现逐字输出的方法
2019/01/23 Python
python爬虫之遍历单个域名
2019/11/20 Python
Python应用实现处理excel数据过程解析
2020/06/19 Python
python3 中时间戳、时间、日期的转换和加减操作
2020/07/14 Python
Python扫描端口的实现
2021/01/25 Python
e路東瀛(JAPANiCAN)香港:日本旅游、日本酒店和温泉旅馆预订
2018/11/21 全球购物
个人作风建设总结
2014/10/23 职场文书
mysql创建存储过程及函数详解
2021/12/04 MySQL
MySQL创建管理子分区
2022/04/13 MySQL
解决Python保存文件名太长OSError: [Errno 36] File name too long
2022/05/11 Python