Flat 10% & upto 50% off + 10% Cashback + Free additional Courses. Hurry up

Understanding Custom Directive

AngularJS allows you to create custom directives with which it becomes easier to encapsulate and simplify DOM manipulation. These directives extend the HTML functionality. Also new directives can be created to manipulate the HTML behavior.

Following are the ways to implement custom directives in AngularJS :

1. Element directives

It is activated when find a matching HTML element in the HTML template.


2. Attribute directives

It is activated when finds a matching HTML attribute in the template.

<div my-directive></div>

3. CSS Class directives
It is activated when finds a matching CSS Class.

<div class=”my-directive: expression;”></div>

4. Comment directives

It is enabled when finds a matching HTML comment.

<!--  Directive : my-directive expression;  -->


Some of commonly used properties are:

1. restrict

It specify how a directive is implemented in angular app. There are 4 restrict options:

restrict : ‘A’ – attribute (default one) | <div my-directive></div>
restrict : ‘C’ – Class | <div class=”my-directive: expression;”></div>
restrict : ‘E’ – element | <my-directive></my-directive>
restrict : ‘M’ – Comment | <!– Directive : my-directive expression; –>

You can also specify multiple restrict like as follows:

restrict : ‘EC’


2. scope

Scope accesses data or methods inside template and link function. By default the directives do not produce their own scope without explicitly set.

Different types of scopes are able to define which are as follows:

  • scope : true – Get new scope
  • scope : false – Use its parent scope
  • scope : {} – Get new isolated scope that doesn’t inherit from parent and exists on its own


3. template
It specify the HTML content that will be generated when directive is compiled.


4. templateUrl
It  specify the path of template that should be used by directives.

details.directive('intellipaat', function () {
return {
restrict: 'E',
scope: false,
templateUrl: 'intellipaat'.tpl.html'





<title>Angular JS Custom Directives</title>



<h1>AngularJS Custom Directives </h1>

<div ng-app = "detailsApp" ng-controller = "employeeController">

<employee name = "Mahesh"></employee><br/>

<employee name = "Piyush"></employee>


<script src = ""></script>


var detailsApp= angular.module("detailsApp", []);

detailsApp.directive('employee', function() {

var directive = {};

directive.restrict = 'E';

directive.template = "employee: <b>{{}}</b> , Id: <b>{{}}</b>";

directive.scope = {

employee: "=name"


directive.compile = function(element, attributes) {

element.css("border", "1px solid #cccccc");

var linkFunction = function($scope, element, attributes) {

element.html("Employee: <b>"+$ +"</b> , Id: <b>"+$"</b><br/>");

element.css("background-color", "#ffffff");


return linkFunction;


return directive;


detailsApp.controller('employeeController', function($scope) {

$scope.Mahesh = {};

$ = "abc";

$ = 1;

$scope.Piyush = {};

$ = "xyz";

$ = 2;






AngularJS Custom Directives

Employee: abc , Id: 1

Employee: xyz , Id: 2

Previous Next

Download Interview Questions asked by top MNCs in 2019?

"0 Responses on AngularJS Custom Directives"

    100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

    Sales Offer

    Sign Up or Login to view the Free AngularJS Custom Directives.