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重写alert方法的实例代码
Mar 29 Javascript
js向上无缝滚动,网站公告效果 具体代码
Nov 18 Javascript
使用js解决由border属性引起的div宽度问题
Nov 26 Javascript
javascript记录文本框内文字个数检测文字个数变化
Oct 14 Javascript
javascript html5 canvas实现可拖动省份的中国地图
Mar 11 Javascript
AngularJS在IE8的不支持的解决方法
May 13 Javascript
浅谈Web页面向后台提交数据的方式和选择
Sep 23 Javascript
js实现点击图片自动提交action的简单方法
Oct 16 Javascript
vue中子组件传递数据给父组件的讲解
Jan 27 Javascript
详解无限滚动插件vue-infinite-scroll源码解析
May 12 Javascript
es6中比较有用的7个技巧小结
Jul 12 Javascript
js+canvas实现图片格式webp/png/jpeg在线转换
Aug 22 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学习 变量使用总结
2011/03/24 PHP
通过缓存数据库结果提高PHP性能的原理介绍
2012/09/05 PHP
php获取本地图片文件并生成xml文件输出具体思路
2013/04/27 PHP
PHP实现简单实用的验证码类
2015/07/29 PHP
用js实现层随着内容大小动态渐变改变 推荐
2009/12/19 Javascript
ExtJS 2.0 GridPanel基本表格简明教程
2010/05/25 Javascript
JavaScript去掉数组中的重复元素
2011/01/13 Javascript
Kibo 用于处理键盘事件的Javascript工具库
2011/10/28 Javascript
Mac/Windows下如何安装Node.js
2013/11/22 Javascript
jQuery ajax提交Form表单实例(附demo源码)
2016/04/06 Javascript
Vue.js报错Failed to resolve filter问题的解决方法
2016/05/25 Javascript
jQuery实现简单的网页换肤效果示例
2016/09/18 Javascript
浅谈js函数的多种定义方法与区别
2016/11/29 Javascript
前端js弹出框组件使用方法
2020/08/24 Javascript
vue2.0项目中使用Ueditor富文本编辑器示例代码
2017/08/14 Javascript
js插件实现图片滑动验证码
2020/09/29 Javascript
JavaScript原型式继承实现方法
2019/11/06 Javascript
ant design实现圈选功能
2019/12/17 Javascript
js实现弹幕墙效果
2020/12/10 Javascript
python 中的divmod数字处理函数浅析
2017/10/17 Python
python opencv摄像头的简单应用
2019/06/06 Python
PyTorch的Optimizer训练工具的实现
2019/08/18 Python
CSS3实现简易版的刮刮乐效果
2016/09/27 HTML / CSS
NICKIS.com荷兰:设计师儿童时装
2020/01/08 全球购物
巴西Bo.Bô官方在线商店:经营奢侈品时尚业务
2020/03/16 全球购物
冰淇淋店创业计划书范文
2013/12/27 职场文书
六月份红领巾广播稿
2014/02/03 职场文书
房地产广告词大全
2014/03/19 职场文书
党员干部形式主义个人整改措施
2014/09/17 职场文书
单位工作证明格式模板
2014/10/04 职场文书
放射科岗位职责
2015/02/14 职场文书
邮政营业员岗位职责
2015/04/14 职场文书
2016七夕情人节寄语
2015/12/04 职场文书
2016年艾滋病宣传活动总结
2016/04/01 职场文书
民政局2016年“六一”儿童节慰问活动总结
2016/04/06 职场文书
使用CSS设置滚动条样式
2022/01/18 HTML / CSS