• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >AngularJS > AngularJS 指令的交互详解及实例代码

AngularJS 指令的交互详解及实例代码

作者:xingoo 字体:[增加 减小] 来源:互联网 时间:2017-05-30

本文主要包含angularjs指令,angularjs指令大全,angularjs自定义指令,angularjs内置指令,angularjs中的指令等相关知识,xingoo 希望在学习及工作中可以帮助到您

  背景介绍

  这例子是视频中的例子,有一个动感超人,有三种能力,力量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>
</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);
            });
          }
        }
      });
</div>

  这里不同的是,在方法内部有一个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();
          }
        }
      });
</div>

  三个指令的代码都差不多,其中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>


 </div>

 运行结果:

        以上就是对AngularJS 指令的交互的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

</div>

您可能想查找下面的文章:

  • AngularJS自定义指令之复制指令实现方法
  • AngularJS自定义指令实现面包屑功能完整实例
  • angularjs指令之绑定策略(@、=、&)
  • 详解angularJs指令的3种绑定策略
  • Angularjs 动态添加指令并绑定事件的方法
  • Angularjs中使用指令绑定点击事件的方法
  • 深入理解AngularJS中的ng-bind-html指令
  • 使用AngularJS2中的指令实现按钮的切换效果
  • Angularjs自定义指令实现三级联动 选择地理位置
  • angularJS 指令封装回到顶部示例详解

相关文章

  • 2017-05-30AngularJS基础学习笔记之控制器
  • 2017-05-30详解AngularJS中的表单验证(推荐)
  • 2017-05-30AngularJS 视图详解及示例代码
  • 2017-05-30AngularJS 自定义指令详解及实例代码
  • 2017-05-30angular ng-repeat数组中的数组实例
  • 2017-05-30AngularJS 让人爱不释手的八种功能
  • 2017-05-30AngularJS中的$watch(),$digest()和$apply()区分
  • 2017-05-30Angular2使用Guard和Resolve进行验证和权限控制
  • 2017-05-30关于angularJs指令的Scope(作用域)介绍
  • 2017-05-30Angular 路由route实例代码

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • RequireJS 依赖关系的实例(推荐)
    • 探索angularjs+requirejs全面实现按需加载的套路
    • Angular2使用Angular CLI快速搭建工程(一)
    • 详解AngularJS中的http拦截
    • AngularJS1.X学习笔记2-数据绑定详解
    • AngularJS表单验证中级篇(3)
    • 详解angularjs利用ui-route异步加载组件
    • AngularJS入门教程之Scope(作用域)
    • AngularJS实用开发技巧(推荐)
    • AngularJS入门教程之XHR和依赖注入详解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有