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 相关文章推荐
JavaScript 事件绑定及深入
Apr 13 Javascript
jquery中的工具使用方法$.isFunction, $.isArray(), $.isWindow()
Aug 09 Javascript
JavaScript的函数式编程基础指南
Mar 19 Javascript
AngularJS使用自定义指令替代ng-repeat的方法
Sep 17 Javascript
Angular.JS中的指令引用template与指令当做属性详解
Mar 30 Javascript
Vue.js实战之组件之间的数据传递
Apr 01 Javascript
JS处理数据四舍五入(tofixed与round的区别详解)
Oct 26 Javascript
Vue.js 点击按钮显示/隐藏内容的实例代码
Feb 08 Javascript
layui之select的option叠加问题的解决方法
Mar 08 Javascript
Javasript设计模式之链式调用详解
Apr 26 Javascript
vue中eslintrc.js配置最详细介绍
Dec 21 Javascript
详解用Webpack与Babel配置ES6开发环境
Mar 12 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
php实现的发送带附件邮件类实例
2014/09/22 PHP
详解WordPress开发中get_header()获取头部函数的用法
2016/01/08 PHP
PHP读取mssql json数据中文乱码的解决办法
2016/04/11 PHP
Joomla数据库操作之JFactory::getDBO用法
2016/05/05 PHP
PHP小偷程序的设计与实现方法详解
2016/10/15 PHP
JS左右无缝滚动(一般方法+面向对象方法)
2012/08/17 Javascript
修改js Calendar日历控件 兼容IE9/谷歌/火狐
2013/01/04 Javascript
javascript查询字符串参数的方法
2015/01/28 Javascript
js日期范围初始化得到前一个月日期的方法
2015/05/05 Javascript
JSON与XML优缺点对比分析
2015/07/17 Javascript
javascript动态获取登录时间和在线时长
2016/02/25 Javascript
JS中sort函数排序用法实例分析
2016/06/16 Javascript
jquery仿京东侧边栏导航效果
2017/03/02 Javascript
vue项目中用cdn优化的方法
2018/01/03 Javascript
vue实现数字滚动效果
2020/06/29 Javascript
浅谈Vuex的this.$store.commit和在Vue项目中引用公共方法
2020/07/24 Javascript
[36:17]DOTA2上海特级锦标赛 - VGL音乐会全集
2016/03/06 DOTA
[09:59]DOTA2-DPC中国联赛2月7日Recap集锦
2021/03/11 DOTA
python高并发异步服务器核心库forkcore使用方法
2013/11/26 Python
Python写的Tkinter程序屏幕居中方法
2015/03/10 Python
Python 12306抢火车票脚本
2018/02/07 Python
Python去除、替换字符串空格的处理方法
2018/04/01 Python
pandas DataFrame 根据多列的值做判断,生成新的列值实例
2018/05/18 Python
详解python爬虫系列之初识爬虫
2019/04/06 Python
Python动态语言与鸭子类型详解
2019/07/01 Python
代码实例讲解python3的编码问题
2019/07/08 Python
Python检查图片是否损坏及图片类型是否正确过程详解
2019/09/30 Python
一些关于python 装饰器的个人理解
2020/08/31 Python
详解python with 上下文管理器
2020/09/02 Python
Grow Gorgeous美国官网:只要八天,体验唤醒毛囊后新生的茂密秀发
2018/06/04 全球购物
基督教卡片、励志礼品、家居装饰等:DaySpring
2018/10/12 全球购物
新加坡一家在线男士皮具品牌:Faire Leather Co.
2019/12/01 全球购物
JS原生实现轮播图的几种方法
2021/03/23 Javascript
幼儿教师考核制度
2014/01/25 职场文书
腾讯广告词
2014/03/19 职场文书
廉洁校园实施方案
2014/05/25 职场文书