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

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…
11/10/2025
Why Custom Shopify Website Development Is the Key to Building a Scalable eCommerce Brand
Introduction: The Rise of Shopify in Modern eCommerce In today’s fast-paced digital marketplace, where customer…
07/10/2025
Shopify Development for Enterprise eCommerce: A Complete Guide for Large-Scale Brands
When your business crosses a certain scale—many SKUs, high order volume, multiple sales channels, global…
03/10/2025
Shopify Theme Development vs Custom Shopify Website Design: What’s Best for Your Business?
Introduction When launching or upgrading an online store, one of the most important decisions you’ll…

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!