Hello folks! welcome back to a new edition of our tutorial on AngularJS. In this tutorial, we're going to be studying about AngularJS Custom Directives.
Custom directives are used in AngularJS to extend the functionalities of HTML. Custom directives are defined via 'directive' function. A custom directive replaces the element for which it is activated. AngularJS application during the config phase finds the matching elements and do one time activity using its compile() method of the custom derivative and then process the elements making use of the link() method of the custom directive based on the scope of the directive.
AngularJS supports the creation of custom directives for following types of elements -
Custom directives are used in AngularJS to extend the functionalities of HTML. Custom directives are defined via 'directive' function. A custom directive replaces the element for which it is activated. AngularJS application during the config phase finds the matching elements and do one time activity using its compile() method of the custom derivative and then process the elements making use of the link() method of the custom directive based on the scope of the directive.
AngularJS supports the creation of custom directives for following types of elements -
- Attribute Directive - It activates when a matching attribute is encountered.
- Element Directive - It activates when a matching element is encountered.
- Comment Directive - It activates when a matching comment is encountered.
Understanding Custom Directive
Define custom html tags.
<student name = "Kennedy"></student><br/> <student name = "Bethel"></student>
Define custom directive to handle the above custom html tags.
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; });
Define controller to update the scope for the directive. Here are utilizing name attribute's value as scope's child.
mainApp.controller('StudentController', function($scope) { $scope.Kennedy = {}; $scope.Kennedy.name = "Kennedy Nkpara"; $scope.Kennedy.rollno = 1; $scope.Bethel = {}; $scope.Bethel.name = "Bethel Igwela"; $scope.Bethel.rollno = 2; });
Example
Following below is a simple example -
<html> <head> <title>Angular JS Custom Directives</title> </head> <body> <h2>AngularJS Sample Application</h2> <div ng-app = "mainApp" ng-controller = "StudentController"> <student name = "Kennedy"></student><br/> <student name = "Bethel"></student> </div> <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/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.Kennedy = {}; $scope.Kennedy.name = "Kennedy Nkpara"; $scope.Kennedy.rollno = 1; $scope.Bethel = {}; $scope.Bethel.name = "Bethel Igwela"; $scope.Bethel.rollno = 2; }); </script> </body> </html>
Output
Open the file testAngularJS.html in any web browser to see the result -
AngularJS Sample Application
READ: AngularJS | Scopes
Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we are going to be studying about AngularJS Internationalization.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.