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操作cookie的文章(设置,删除cookies)
Apr 01 Javascript
js实现单一html页面两套css切换代码
Apr 11 Javascript
javascript实现TreeView 无刷新展开的实例代码
Jul 13 Javascript
利用javascript打开模态对话框(示例代码)
Jan 11 Javascript
jquery 重写 ajax提交并判断权限后 使用load方法报错解决方法
Jan 19 Javascript
javascript事件处理模型实例说明
May 31 Javascript
AngularJS入门教程之REST和定制服务详解
Aug 19 Javascript
BootStrap中关于Select下拉框选择触发事件及扩展
Nov 22 Javascript
js实现本地图片文件拖拽效果
Jul 18 Javascript
D3.js的基础部分之数组的处理数组的排序和求值(v3版本)
May 09 Javascript
JavaScript在web自动化测试中的作用示例详解
Aug 25 Javascript
Vue登录拦截 登录后继续跳转指定页面的操作
Aug 04 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
WHOIS类的修改版
2006/10/09 PHP
PHP 已经成熟
2006/12/04 PHP
解析php根据ip查询所在地区(非常有用,赶集网就用到)
2013/07/01 PHP
ThinkPHP实现一键清除缓存方法
2014/06/26 PHP
php单链表实现代码分享
2016/07/04 PHP
Laravel框架处理用户的请求操作详解
2019/12/20 PHP
一段利用WSH获取登录时间的jscript代码
2008/05/11 Javascript
javascript 鼠标悬浮图片显示原图 移出鼠标后原图消失(多图)
2009/12/28 Javascript
JavaScript函数作用域链分析
2015/02/13 Javascript
SpringMVC返回json数据的三种方式
2015/12/10 Javascript
详解Javascript中的Object对象
2016/02/28 Javascript
Node.js利用Net模块实现多人命令行聊天室的方法
2016/12/23 Javascript
Angular2库初探
2017/03/01 Javascript
深究AngularJS中ng-drag、ng-drop的用法
2017/06/12 Javascript
vue-cli 组件的导入与使用教程详解
2018/04/11 Javascript
jQuery实现网页拼图游戏
2020/04/22 jQuery
JavaScript基础之this和箭头函数详析
2019/09/05 Javascript
谈谈我在vue-cli3中用预渲染遇到的坑
2020/04/22 Javascript
JS+CSS实现动态时钟
2021/02/19 Javascript
对python 读取线的shp文件实例详解
2018/12/22 Python
python网络应用开发知识点浅析
2019/05/28 Python
django框架forms组件用法实例详解
2019/12/10 Python
Python enumerate函数遍历数据对象组合过程解析
2019/12/11 Python
解决jupyter notebook显示不全出现框框或者乱码问题
2020/04/09 Python
CSS3 RGBA色彩模式使用实例讲解
2016/04/26 HTML / CSS
猎人靴英国官网:Hunter Boots
2017/02/02 全球购物
美国最流行的男士时尚网站:Touch of Modern
2018/02/05 全球购物
经济管理专业自荐信
2013/12/30 职场文书
销售员岗位职责范本
2014/02/03 职场文书
家长会主持词开场白
2014/03/18 职场文书
高中教师评语大全
2014/04/25 职场文书
财务人员担保书
2014/05/13 职场文书
学校做一个有道德的人活动方案
2014/08/23 职场文书
代办出身证明书
2014/10/21 职场文书
入党申请书格式
2019/06/20 职场文书
Keras多线程机制与flask多线程冲突的解决方案
2021/05/28 Python