AngularJS 自定义指令详解及示例代码


Posted in Javascript onAugust 17, 2016

自定义指令中使用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 自定义指令详解及示例代码

以上就是对AngularJS的自定义指令资料整理,后续继续补充,谢谢大家对本站的支持!

Javascript 相关文章推荐
用roll.js实现的图片自动滚动+鼠标触动的特效
Mar 18 Javascript
JS 参数传递的实际应用代码分析
Sep 13 Javascript
javascript中的float运算精度实例分析
Aug 21 Javascript
js创建数据共享接口——简化框架之间相互传值
Oct 23 Javascript
上传图片js判断图片尺寸和格式兼容IE
Sep 01 Javascript
jQuery解决$符号命名冲突
Jun 18 Javascript
ES6新特性五:Set与Map的数据结构实例分析
Apr 21 Javascript
bootstrap弹出层的多种触发方式
May 10 Javascript
基于Vue过渡状态实例讲解
Sep 14 Javascript
js实现控制文件拖拽并获取拖拽内容功能
Feb 17 Javascript
Vue波纹按钮组件制作
Apr 30 Javascript
node.js Promise对象的使用方法实例分析
Dec 26 Javascript
AngularJS 依赖注入详解及示例代码
Aug 17 #Javascript
AngularJS 服务详细讲解及示例代码
Aug 17 #Javascript
深入理解jQuery layui分页控件的使用
Aug 17 #Javascript
AngularJS 作用域详解及示例代码
Aug 17 #Javascript
jQuery Easyui使用(一)之可折叠面板的布局手风琴菜单
Aug 17 #Javascript
jQuery Easyui使用(二)之可折叠面板动态加载无效果的解决方法
Aug 17 #Javascript
js实现添加可信站点、修改activex安全设置,禁用弹出窗口阻止程序
Aug 17 #Javascript
You might like
php导出csv数据在浏览器中输出提供下载或保存到文件的示例
2014/04/24 PHP
ThinkPHP表单自动提交验证实例教程
2014/07/18 PHP
浅谈PHP的exec()函数无返回值排查方法(必看)
2017/03/31 PHP
详解php语言最牛掰的Laravel框架
2017/11/20 PHP
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
纯CSS打造的导航菜单(附jquery版)
2010/08/07 Javascript
javascript常见用法总结
2014/05/22 Javascript
JQuery实现鼠标移动图片显示描述层的方法
2015/06/25 Javascript
详细分析JavaScript变量类型
2015/07/08 Javascript
JavaScript中创建对象的模式汇总
2016/04/19 Javascript
JavaScript中自带的 reduce()方法使用示例详解
2016/08/10 Javascript
react+ant design实现Table的增、删、改的示例代码
2018/12/27 Javascript
vue实现的网易云音乐在线播放和下载功能案例
2019/02/18 Javascript
js取0-9随机取4个数不重复的数字代码实例
2019/03/27 Javascript
vue element-ui之怎么封装一个自己的组件的详解
2019/05/20 Javascript
[50:28]LGD女子学院第三期 DOTA2复仇之魂教学
2013/12/24 DOTA
python切换hosts文件代码示例
2013/12/31 Python
python实现在windows下操作word的方法
2015/04/28 Python
深入理解Python中字典的键的使用
2015/08/19 Python
Python正则抓取网易新闻的方法示例
2017/04/21 Python
Python中%是什么意思?python中百分号如何使用?
2018/03/20 Python
wxPython的安装与使用教程
2018/08/31 Python
详解Python 爬取13个旅游城市,告诉你五一大家最爱去哪玩?
2019/05/07 Python
python 输出列表元素实例(以空格/逗号为分隔符)
2019/12/25 Python
python两个_多个字典合并相加的实例代码
2019/12/26 Python
Python常用库大全及简要说明
2020/01/17 Python
python多进程下的生产者和消费者模型
2020/05/07 Python
python操作微信自动发消息的实现(微信聊天机器人)
2020/07/14 Python
什么是GWT的Entry Point
2013/08/16 面试题
个人承诺书怎么写
2014/05/24 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书
社区六一儿童节活动总结
2015/02/11 职场文书
撤诉申请书法院范本
2015/05/18 职场文书
CAD实训总结范文
2015/08/03 职场文书
初中化学教学反思
2016/02/22 职场文书
Spring Boot优化后启动速度快到飞起技巧示例
2022/07/23 Java/Android