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 07 Javascript
asp.net HttpHandler实现图片防盗链
Nov 09 Javascript
分别用marquee和div+js实现首尾相连循环滚动效果,仅3行代码
Sep 21 Javascript
js获取url参数代码实例分享(JS操作URL)
Dec 13 Javascript
同一个网页中实现多个JavaScript特效的方法
Feb 02 Javascript
纯js实现瀑布流布局及ajax动态新增数据
Apr 07 Javascript
HTML5+jQuery插件Quicksand实现超酷的星际争霸2兵种分类展示效果(附demo源码下载)
May 25 Javascript
js基于setTimeout与setInterval实现多线程
Jun 17 Javascript
Bootstrap超大屏幕的实现代码
Mar 22 Javascript
随机生成10个不重复的0-100的数字(实例讲解)
Aug 16 Javascript
JS造成内存泄漏的几种情况实例分析
Mar 02 Javascript
如何vue使用el-table遍历循环表头和表体数据
Apr 26 Vue.js
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
索尼SONY ICF-SW7600GR电路分析与改良
2021/03/02 无线电
通过PHP修改Linux或Unix口令的方法分享
2012/01/30 PHP
php修改指定文件后缀的方法
2014/09/11 PHP
PHP获取数组最后一个值的2种方法
2015/01/21 PHP
php简单处理XML数据的方法示例
2017/05/19 PHP
PHP实现找出有序数组中绝对值最小的数算法分析
2017/08/07 PHP
JavaScript 产生不重复的随机数三种实现思路
2012/12/13 Javascript
JS刷新框架外页面七种实现代码
2013/02/18 Javascript
jqGrid读取选择的多行的某个属性代码
2014/05/18 Javascript
js实现仿网易点击弹出提示同时背景变暗效果
2015/08/13 Javascript
js实现倒计时关键代码
2017/05/05 Javascript
Angular模板表单校验方法详解
2017/08/11 Javascript
layer.open 获取不到表单信息的解决方法
2019/09/26 Javascript
webpack 最佳配置指北(推荐)
2020/01/07 Javascript
vue-resourc发起异步请求的方法
2020/02/11 Javascript
[00:48]完美“圣”典2016风云人物:xiao8宣传片
2016/11/30 DOTA
Python利用splinter实现浏览器自动化操作方法
2018/05/11 Python
python打包生成的exe文件运行时提示缺少模块的解决方法
2018/10/31 Python
对python 命令的-u参数详解
2018/12/03 Python
Python实现计算字符串中出现次数最多的字符示例
2019/01/21 Python
Python命令行参数解析工具 docopt 安装和应用过程详解
2019/09/26 Python
python实现监控阿里云账户余额功能
2019/12/16 Python
Python pandas库中的isnull()详解
2019/12/26 Python
Keras 中Leaky ReLU等高级激活函数的用法
2020/07/05 Python
python利用google翻译方法实例(翻译字幕文件)
2020/09/21 Python
css3进行截取替代js的substring
2013/09/02 HTML / CSS
在HTML5中如何使用CSS建立不可选的文字
2014/10/17 HTML / CSS
TUMI马来西亚官方网站:国际领先的高品质商旅箱包品牌
2018/04/26 全球购物
为奢侈时尚带来了慈善元素:Olivela
2018/09/29 全球购物
美国手机支架公司:PopSockets
2019/11/27 全球购物
荷兰时尚精品店:Labels Fashion
2020/03/22 全球购物
俄罗斯童装网上商店:BebaKids
2020/06/06 全球购物
网络编程中设计并发服务器,使用多进程与多线程,请问有什么区别?
2016/03/27 面试题
《中国的气候》教学反思
2014/02/23 职场文书
铁拳制作人赞《铁拳7》老头环Mod:制作精良 但别弄了
2022/04/03 其他游戏
Navicat Premium自定义 sql 标签的创建方式
2022/09/23 数据库