源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

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

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2016-8-17 15:38:25 | 显示全部楼层 |阅读模式
本文主要介绍AngularJS 自定义指令,这里整理了基础资料及详细的示例代码及实现效果图,有需要的小伙伴可以参考下

自定义指令中使用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的自定义指令资料整理,后续继续补充,谢谢大家对本站的支持!

回复

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-8-12 06:11:17 | 显示全部楼层
啪啪啪生怕PSP怕
回复 支持 反对

使用道具 举报

2

主题

2万

回帖

381

积分

中级会员

Rank: 3Rank: 3

积分
381
发表于 2022-9-26 01:13:49 | 显示全部楼层
挺不错的东西
回复 支持 反对

使用道具 举报

0

主题

8878

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-10-26 21:08:00 | 显示全部楼层
这个源码还可以
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-9-3 08:21:59 | 显示全部楼层
谢谢小Y分享
回复 支持 反对

使用道具 举报

1

主题

2万

回帖

362

积分

中级会员

Rank: 3Rank: 3

积分
362
发表于 2023-9-3 21:47:15 | 显示全部楼层
看看看看
回复 支持 反对

使用道具 举报

7

主题

2万

回帖

398

积分

中级会员

Rank: 3Rank: 3

积分
398
发表于 2023-10-29 18:45:11 | 显示全部楼层
天天源码社区www.tiantianym.com
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-11-25 00:31:57 | 显示全部楼层
论坛有你更精彩!
回复 支持 反对

使用道具 举报

11

主题

2万

回帖

300

积分

中级会员

Rank: 3Rank: 3

积分
300
发表于 2024-2-22 23:42:48 | 显示全部楼层
逛逛看看瞧瞧
回复 支持 反对

使用道具 举报

15

主题

2万

回帖

122

积分

注册会员

Rank: 2

积分
122
发表于 2024-4-7 05:36:45 | 显示全部楼层
啊,数码撒飒飒飒飒
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-6 11:40 , Processed in 0.152889 second(s), 23 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表