详解AngularJS中自定义指令的使用


Posted in Javascript onJune 17, 2015

 自定义指令中使用AngularJS扩展HTML的功能。自定义指令使用的“指令”的功能定义。自定义指令只是替换了它被激活的元素。引导过程中AngularJS应用程序找到了匹配的元素,并做好使用自定义指令compile()方法一次活动再处理使用基于指令的范围自定义指令link()方法的元素。 AngularJS提供支持,以下列元素的类型来创建自定义指令。

  •     Element directives - 指令遇到时激活一个匹配的元素。
  •     Attribute - - 指令遇到时激活一个匹配的属性。
  •     CSS - - 指令遇到时激活匹配CSS样式。
  •     Comment - - 指令遇到时激活匹配的注释。

了解自定义指令

定义自定义的HTML标签。

<student name="Mahesh"></student><br/>
<student name="Piyush"></student>

定义自定义指令来处理上面的自定义HTML标签。

var mainApp = angular.module("mainApp", []);

//Create a directive, first parameter is the html element to be attached.  
//We are attaching student html tag. 
//This directive will be activated as soon as any student element is encountered in html
mainApp.directive('student', function() {
  //define the directive object
  var directive = {};
  //restrict = E, signifies that directive is Element directive
  directive.restrict = 'E';
  //template replaces the complete element with its text. 
  directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";
  //scope is used to distinguish each student element based on criteria.
  directive.scope = {
    student : "=name"
  }
  //compile is called during application initialization. AngularJS calls it once when html page is loaded.
  directive.compile = function(element, attributes) {
   element.css("border", "1px solid #cccccc");
  //linkFunction is linked with each element with scope to get the element specific data.
   var linkFunction = function($scope, element, attributes) {
     element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>");
     element.css("background-color", "#ff00ff");
   }
   return linkFunction;
  }
  return directive;
});

定义控制器以更新范围为指令。在这里,我们使用name属性值作为子的作用域。

mainApp.controller('StudentController', function($scope) {
   $scope.Mahesh = {};
   $scope.Mahesh.name = "Mahesh Parashar";
   $scope.Mahesh.rollno = 1;

   $scope.Piyush = {};
   $scope.Piyush.name = "Piyush Parashar";
   $scope.Piyush.rollno = 2;
});

例子

<html>
<head>
  <title>Angular JS Custom Directives</title>
</head>
<body>
  <h2>AngularJS Sample Application</h2>
  <div ng-app="mainApp" ng-controller="StudentController">
 <student name="Mahesh"></student><br/>
 <student name="Piyush"></student>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
  <script>
   var mainApp = angular.module("mainApp", []);
  
   mainApp.directive('student', function() {
     var directive = {};
     directive.restrict = 'E';
     directive.template = "Student: <b>{{student.name}}</b> , Roll No: <b>{{student.rollno}}</b>";
     
     directive.scope = {
      student : "=name"
     }
  
     directive.compile = function(element, attributes) {
      element.css("border", "1px solid #cccccc");

      var linkFunction = function($scope, element, attributes) {
        element.html("Student: <b>"+$scope.student.name +"</b> , Roll No: <b>"+$scope.student.rollno+"</b><br/>");
        element.css("background-color", "#ff00ff");
      }

      return linkFunction;
     }

     return directive;
   });
  
   mainApp.controller('StudentController', function($scope) {
      $scope.Mahesh = {};
      $scope.Mahesh.name = "Mahesh Parashar";
      $scope.Mahesh.rollno = 1;

      $scope.Piyush = {};
      $scope.Piyush.name = "Piyush Parashar";
      $scope.Piyush.rollno = 2;
   });
   
  </script>
</body>
</html>

结果

在Web浏览器中打开textAngularJS.html。看到结果如下:

详解AngularJS中自定义指令的使用

Javascript 相关文章推荐
jquery validate poshytip 自定义样式
Nov 26 Javascript
jQuery取消ajax请求的方法
Jun 09 Javascript
可以浮动某个物体的jquery控件用法实例
Jul 24 Javascript
coffeescript使用的方式汇总
Aug 05 Javascript
node.js中fs.stat与fs.fstat的区别详解
Jun 01 Javascript
jQuery选择器之子元素过滤选择器
Sep 28 jQuery
JavaScript数据结构之优先队列与循环队列实例详解
Oct 27 Javascript
实现单层json按照key字母顺序排序的示例
Dec 06 Javascript
Angular实现下拉框模糊查询功能示例
Jan 03 Javascript
使用vuex的state状态对象的5种方式
Apr 19 Javascript
JavaScript中set与get方法用法示例
Aug 15 Javascript
Angular7中创建组件/自定义指令/管道的方法实例详解
Apr 02 Javascript
详解AngularJS中的依赖注入机制
Jun 17 #Javascript
详解AngularJS中的作用域
Jun 17 #Javascript
简介AngularJS中使用factory和service的方法
Jun 17 #Javascript
简介AngularJS的视图功能应用
Jun 17 #Javascript
在AngularJS中使用AJAX的方法
Jun 17 #Javascript
使用AngularJS来实现HTML页面嵌套的方法
Jun 17 #Javascript
AngularJS的表单使用详解
Jun 17 #Javascript
You might like
深入php内核之php in array
2015/11/10 PHP
PHP中spl_autoload_register()函数用法实例详解
2016/07/18 PHP
基于jQuery实现下拉收缩(展开与折叠)特效
2012/12/25 Javascript
JavaScript自定义日期格式化函数详细解析
2014/01/14 Javascript
JQuery中extend使用介绍
2014/03/13 Javascript
jquery中show()、hide()和toggle()用法实例
2015/01/15 Javascript
JavaScript String 对象常用方法总结
2016/04/28 Javascript
checkbox 选中一个另一个checkbox也会选中的实现代码
2016/07/09 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
Angular2 组件间通过@Input @Output通讯示例
2017/08/24 Javascript
nodejs使用express获取get和post传值及session验证的方法
2017/11/09 NodeJs
SpringBoot+Vue前后端分离,使用SpringSecurity完美处理权限问题的解决方法
2018/01/09 Javascript
使用layer弹窗和layui表单实现新增功能
2018/08/09 Javascript
详解Vue中的scoped及穿透方法
2019/04/18 Javascript
JS中类的静态方法,静态变量,实例方法,实例变量区别与用法实例分析
2020/03/14 Javascript
vuex中store存储store.commit和store.dispatch的用法
2020/07/24 Javascript
vue-preview动态获取图片宽高并增加旋转功能的实现
2020/07/29 Javascript
Python实现类似jQuery使用中的链式调用的示例
2016/06/16 Python
机器学习python实战之决策树
2017/11/01 Python
Pycharm远程调试openstack的方法
2017/11/21 Python
python一行sql太长折成多行并且有多个参数的方法
2018/07/19 Python
pytorch 固定部分参数训练的方法
2019/08/17 Python
Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法
2019/08/26 Python
python在不同条件下的输入与输出
2020/02/13 Python
tensorflow实现从.ckpt文件中读取任意变量
2020/05/26 Python
Python带参数的装饰器运行原理解析
2020/06/09 Python
PyQt5-QDateEdit的简单使用操作
2020/07/12 Python
怎样实现H5+CSS3手指滑动切换图片的示例代码
2019/05/05 HTML / CSS
Pam & Gela官网:美国性感前卫女装品牌
2018/07/19 全球购物
MONNIER Frères英国官网:源自巴黎女士奢侈品配饰电商平台
2018/12/06 全球购物
Linux的文件类型
2016/07/05 面试题
制药工程专业个人求职自荐信
2014/01/25 职场文书
高中生家长寄语大全
2014/04/03 职场文书
上课睡觉万能检讨书
2015/02/17 职场文书
《废话连篇——致新手》——chinapizza
2022/04/05 无线电
《仙剑客栈2》第一弹正式宣传片公开 年内发售
2022/04/07 其他游戏