AngularJS Custom Directives

AngularJS Custom Directives: The AngularJS allows creating custom directives that make it easy for DOM manipulation. The HTML functionality is extended by these directives and also allows to create new directives to manipulate HTML behavior.

AngularJS Custom Directives

Element Directives


The above code gets activated when the matching HTML elements found in the HTML template.

Attribute Directives

<div my-directive></div>

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

CSS Class Directives

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

It will get activated with the matching CSS class.

Comment Directives

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

When there is a matching HTML comment it will be enabled.

Custom Directive Commonly used Properties


The restrict defines how the directive can be implemented in the angular js app. The 4 restrict options are as follows:

restrict Type Description Syntax
restrict  ‘A’ For Attribute which is default <div my-directive></div>
restrict ‘C’ For Class <div class=”my-directive: expression;”></div>
restrict ‘E’ For Element <my-directive></my-directive>
restrict ‘M’ For Comment <!– Directive : my-directive expression; –>

NOTE: Multiple restrict can also be used as restrict ‘EC’.


The scopes are used to access the data or methods which are specified inside the template and link function. Default the directives don’t provide their own space without explicitly set.

Types of scopes

  • Scope:true- To get new scope.
  • Scope: false- To use its parent scope.
  • Scope:{}- to get a new scope that doesn’t inherit from parent and exists on its own.


A template is used to specify the HTML content that generates when a directive has complied.


The templateUrl is used to define the path of the template used by directives.

templateUrl Example

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

AngularJS Custom Directive Example

<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 Directive Example Output

AngularJS Custom Directives
Employee: abc, Id: 1
Employee: xyz, Id: 2