详解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 相关文章推荐
Javascript Global对象
Aug 13 Javascript
javascript 函数调用规则
Aug 26 Javascript
jquery实现先淡出再折叠收起的动画效果
Aug 07 Javascript
jQuery实现宽屏图片轮播实例教程
Nov 24 Javascript
jQuery mobile转换url地址及获取url中目录部分的方法
Dec 04 Javascript
Vue自定义指令拖拽功能示例
Feb 17 Javascript
node.js入门学习之url模块
Feb 25 Javascript
AngularJS实现进度条功能示例
Jul 05 Javascript
vue计算属性时v-for处理数组时遇到的一个bug问题
Jan 21 Javascript
Javascript实现购物车功能的详细代码
May 08 Javascript
vue项目如何刷新当前页面的方法
May 18 Javascript
使用wxapp-img-loader自定义组件实现微信小程序图片预加载功能
Oct 18 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 SQL防注入代码集合
2008/04/25 PHP
PHP全概率运算函数(优化版) Webgame开发必备
2011/07/04 PHP
深入php self与$this的详解
2013/06/08 PHP
Yii框架中 find findAll 查找出制定的字段的方法对比
2014/09/10 PHP
PHP实现PDO的mysql数据库操作类
2014/12/12 PHP
Laravel 5.4因特殊字段太长导致migrations报错的解决
2017/10/22 PHP
ExtJs使用总结(非常详细)
2012/03/22 Javascript
再次谈论Javascript中的this
2016/06/23 Javascript
JS实现六边形3D拖拽翻转效果的方法
2016/09/11 Javascript
不使用script导入js文件的几种方法
2016/10/27 Javascript
基于layer.js实现收货地址弹框选择然后返回相应的地址信息
2017/05/26 Javascript
基于Vue.js实现tab滑块效果
2017/07/23 Javascript
JS实现键值对遍历json数组功能示例
2018/05/30 Javascript
基于vue实现可搜索下拉框定制组件
2020/03/26 Javascript
js中Array对象的常用遍历方法详解
2019/01/17 Javascript
微信小程序自定义tabbar custom-tab-bar 6s出不来解决方案(cover-view不兼容)
2019/11/01 Javascript
[07:20]2018DOTA2国际邀请赛寻真——逐梦Mineski
2018/08/10 DOTA
对python中执行DOS命令的3种方法总结
2018/05/12 Python
Python IDLE清空窗口的实例
2018/06/25 Python
利用python Selenium实现自动登陆京东签到领金币功能
2019/10/31 Python
python3图片文件批量重命名处理
2019/10/31 Python
Python 合并拼接字符串的方法
2020/07/28 Python
浅析python 字典嵌套
2020/09/29 Python
html5 桌面提醒:Notifycations应用介绍
2012/11/27 HTML / CSS
Sunglasses Shop荷兰站:英国最大的太阳镜独立在线零售商和供应商
2017/01/08 全球购物
几道数据库的面试题或笔试题
2014/05/31 面试题
年度考核自我鉴定
2013/11/09 职场文书
单位在职证明范本
2014/01/09 职场文书
11月红领巾广播稿
2014/01/17 职场文书
数学教师个人工作总结
2015/02/06 职场文书
2015年社区关工委工作总结
2015/04/03 职场文书
2015国庆节感想
2015/08/04 职场文书
高中化学教学反思
2016/02/22 职场文书
导游词之青岛太清宫
2019/12/13 职场文书
python 实现图片特效处理
2022/04/03 Python
讲解MySQL增删改操作
2022/05/06 MySQL