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 相关文章推荐
QUnit jQuery的TDD框架
Nov 04 Javascript
jQuery EasyUI API 中文文档 DateTimeBox日期时间框
Oct 16 Javascript
js实现的GridView即表头固定表体有滚动条且可滚动
Feb 19 Javascript
js读写json文件实例代码
Oct 21 Javascript
jQuery元素属性操作实例(设置、获取及删除元素属性)
Sep 08 Javascript
bootstrap下拉列表与输入框组结合的样式调整
Oct 08 Javascript
CSS+jQuery实现简单的折叠菜单
Dec 20 Javascript
详解VUE 数组更新
Dec 16 Javascript
浅谈Javascript常用正则表达式应用
Mar 08 Javascript
js判断在哪个浏览器打开项目的方法
Jan 21 Javascript
原生JS实现贪吃蛇小游戏
Mar 09 Javascript
JS新手入门数组处理的实用方法汇总
Apr 07 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 清空varnish 缓存的详解(包括指定站点下的)
2013/06/20 PHP
Yii使用Captcha验证码的方法
2015/12/28 PHP
PHP函数按引用传递参数及函数可选参数用法示例
2018/06/04 PHP
JQuery Ajax通过Handler访问外部XML数据的代码
2010/06/01 Javascript
子窗体与父窗体传值示例js代码
2013/08/01 Javascript
jquery插件qrcode在线生成二维码
2015/04/26 Javascript
JS实现新浪博客左侧的Blog管理菜单效果代码
2015/10/22 Javascript
移动端Ionic App 资讯上下循环滚动的实现代码(跑马灯效果)
2017/08/29 Javascript
jQuery实现手机号正则验证输入及自动填充空格功能
2018/01/02 jQuery
详解基于vue-cli3.0如何构建功能完善的前端架子
2018/10/09 Javascript
Vue数据双向绑定的深入探究
2018/11/27 Javascript
微信小程序实现富文本图片宽度自适应的方法
2019/01/20 Javascript
在vue中使用G2图表的示例代码
2019/03/19 Javascript
react 组件传值的三种方法
2019/06/03 Javascript
JS开发自己的类库实例分析
2019/08/28 Javascript
微信小程序实现侧边栏分类
2019/10/21 Javascript
原生JS实现拖拽功能
2020/12/16 Javascript
vue 使用 sortable 实现 el-table 拖拽排序功能
2020/12/26 Vue.js
[02:00]最后,我终于出了辉耀
2018/03/27 DOTA
Python内置函数bin() oct()等实现进制转换
2012/12/30 Python
PyQt 线程类 QThread使用详解
2017/07/16 Python
Python OpenCV 直方图的计算与显示的方法示例
2018/02/08 Python
Python基础教程之内置函数locals()和globals()用法分析
2018/03/16 Python
Python 处理图片像素点的实例
2019/01/08 Python
通过python的matplotlib包将Tensorflow数据进行可视化的方法
2019/01/09 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2019/11/21 Python
python时间序列数据转为timestamp格式的方法
2020/08/03 Python
Python中Qslider控件实操详解
2021/02/20 Python
使用html5 canvas 画时钟代码实例分享
2015/11/11 HTML / CSS
马来西亚演唱会订票网站:StubHub马来西亚
2018/10/18 全球购物
村委会主任先进事迹
2014/01/15 职场文书
会计电算化应届生自荐信
2014/02/25 职场文书
创先争优一句话承诺
2014/05/29 职场文书
五一劳动节演讲稿
2014/09/12 职场文书
青年岗位能手事迹材料
2014/12/23 职场文书
MySQL GRANT用户授权的实现
2021/06/18 MySQL