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

 更新时间:2015年06月17日 11:36:27   投稿:goldensun  
这篇文章主要介绍了详解AngularJS中自定义指令的使用,包括结合自定义HTML标签的使用,需要的朋友可以参考下

 自定义指令中使用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。看到结果如下:

2015617113318563.jpg (560×240)

相关文章

  • AngularJS基础 ng-class-odd 指令示例

    AngularJS基础 ng-class-odd 指令示例

    本文主要介绍AngularJS ng-class-odd 指令,这里对ng-class-odd基础知识做了详细整理,并有示例代码和效果图,学习AngularJS的同学可以参考下
    2016-08-08
  • Angularjs实现上传图片预览功能

    Angularjs实现上传图片预览功能

    本文通过实例代码给大家介绍了Angularjs实现上传图片预览功能,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-09-09
  • 详解Angularjs filter过滤器

    详解Angularjs filter过滤器

    这篇文章主要介绍了angularjs filter过滤器的相关资料,需要的朋友可以参考下
    2016-02-02
  • AngularJs中route的使用方法和配置

    AngularJs中route的使用方法和配置

    angular是Google开发的一个单页面应用框架,是现在比较主流的单页面应用框架之一,下面通过本文给大家介绍AngularJs中route的使用方法和配置,感兴趣的朋友一起学习吧
    2016-02-02
  • angular安装import echarts from‘echarts‘标红报错解决

    angular安装import echarts from‘echarts‘标红报错解决

    这篇文章主要介绍了angular安装import echarts from‘echarts‘标红报错解决,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-10-10
  • AngularJS入门教程之静态模板详解

    AngularJS入门教程之静态模板详解

    本文主要介绍AngularJS 静态模板,这里整理了相关的基础资料,会帮助大家学习基础的AngularJS的基础知识,有需要的小伙伴可以参考下
    2016-08-08
  • AngularJS基础知识笔记之表格

    AngularJS基础知识笔记之表格

    这篇文章主要介绍了AngularJS基础知识笔记之表格的相关资料,需要的朋友可以参考下
    2015-05-05
  • 详解AngularJs中$resource和restfu服务端数据交互

    详解AngularJs中$resource和restfu服务端数据交互

    之前小编和大家分享过使用$http同服务器进行通信,但是功能上比较简单,angularjs还提供了另外一个可选的服务$resource,使用它可以非常方便的同支持restful的服务单进行数据交互。下面来一起看看吧。
    2016-09-09
  • angular实现表单验证及提交功能

    angular实现表单验证及提交功能

    这篇文章主要为大家详细介绍了angular实现表单验证及提交功能的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • Angular 4依赖注入学习教程之组件服务注入(二)

    Angular 4依赖注入学习教程之组件服务注入(二)

    大家都知道依赖注入式AngularJS的重要特性之一,之前我们已经介绍了关于Angular 4依赖注入基础的内容,下面这篇文章主要给大家介绍了关于Angular 4依赖注入之组件服务注入的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-06-06

最新评论