Banner Image
Banner Image
Banner Image

Create a custom directive in Angular JS

Angular Js uses MVW(Model View whatever) Architecture. We can create a dynamic pages using Angular Js. We can extend the HTML functionality using Angular JS directive method. We can extend the HTML with four types

  • Element – <my-dir></my-dir>
  • Attribute – <div my-dir=”example”></div>
  • CSS – <div class=”my-dir”></div>
  • Comment

$compile method match the directives based on element, attribute, CSS and comment. Below code will restrict Attribute, Element and CSS

  • ‘A’ – only matches attribute name
  • ‘E’ – only matches element name
  • ‘C’ – only matches class name
  • ‘M’ – only matches comment

We can add the restrict as AEC in the directive

Script.js

[javascript]
angular.module(‘myDirective’, [])
.controller(‘Controller’, [‘$scope’, function($scope) {
$scope.student = {
name: ‘Foo’,
age: ’16’
};
}])
.directive(‘myDir’, function() {
return {
restrict: ‘AEC’,
templateUrl: ‘my-directive.html’
};
});
[/javascript]

index.html

[html]
<div ng-controller="Controller">
<my-dir></my-dir>
</div>
[/html]

my-dir.html

[html]
Name: {{student.name}}
Age: {{student.age}}
[/html]

Result :

[html]
Name: Foo
Age: 16
[/html]

Blog

25/11/2025
How Google’s New AI Shopping Revolution Makes Online Buying Easier — And What It Means for Your Business
Introduction The holiday shopping season is here — and Google is making it smarter. With…
27/10/2025
The Rise of Atlas Browser: Why More Users Are Switching to a Privacy-First Web
In today’s digital world, almost everything we do — from searching for information to online…
24/10/2025
Understanding WordPress: A Simple Guide for Beginners in 2025
If you’ve ever wondered how people build websites without knowing how to code, the answer…
15/10/2025
Building a Robust eCommerce Website with Magento: A Complete Guide by Sharesoft
In today’s fast-moving digital world, building a strong online presence is more than just having…
Bulb

Stay Ahead in Digital Innovation

Looking for expert tips, industry insights, and strategies to boost your digital presence? Subscribe to Sharesoft Technology’s weekly LinkedIn newsletter!