SlideShare a Scribd company logo
AngularJS Custom Directive
Presented by
Md. Masud Rana
Topics
- What are directives?
- Angular built in directives.
- Types of directives.
- Convention to write a custom directive.
- Example of directive to start writing.
What are Directives?
Directives are markers on a DOM element
(such as an attribute, element name, comment
or CSS class) that tell AngularJS's HTML
compiler ($compile) to attach a specified
behavior to that DOM element or even
transform the DOM element and its children.
AngularJS Built in Directives
Angular comes with a set of these built-in
directives like
ngBind, ngModel, and ngClass etc.
Conventions to Write a Directive
- Case-sensitive (camelCase) normalized name(e.g.
ngModel.)
- HTML is case-insensitive, so directives in the DOM by
lower-case forms, typically using dash-delimited
attributes on DOM elements (e.g. ng-model)
- Strip x- and data- from the front of the element/attributes
- Convert the :, -, or _-delimited name to camelCase
Directive in HTML Template
<div ng-controller="Controller">
Hello <input ng-model='name'> <hr/>
<span ng-bind="name"></span> <br/>
<span ng:bind="name"></span> <br/>
<span ng_bind="name"></span> <br/>
<span data-ng-bind="name"></span> <br/>
<span x-ng-bind="name"></span> <br/>
</div>
Best Practice
Prefer using the dash-delimited format (e.g. ng-
bind for ngBind). If you want to use an HTML
validating tool, you can instead use the data-
prefixed version (e.g. data-ng-bind for ngBind).
The other forms shown above are accepted for
legacy reasons but we advise you to avoid
them.
Directive Types
1. Element names (HTML Tag)
2. Attributes
3. Class names and
4. Comments
Example of Directive Types
<my-dir></my-dir>
<span my-dir="exp"></span>
<!-- directive: my-dir exp -->
<span class="my-dir: exp;"></span>
Best Practice: Prefer using directives via tag name and
attributes over comment and class names. Doing so
generally makes it easier to determine what directives a
given element matches.
Instruction about Directives
1. Register a directive, use the module.directive
2. Directive name followed by a factory function
3. To avoid collisions with some future standard, it's best to
prefix your own directive names. For instance, if you
created a <carousel> directive, it would be problematic if
HTML7 introduced the same element. A two or three letter
prefix (e.g. btfCarousel) works well.
4. Do not prefix your own directives with ng
Writing Some Custom Directive
1. Replace static content
Demo: https://jsfiddle.net/masudcsesust04/nj25aznt/
2. Restricted directive
Demo: https://jsfiddle.net/masudcsesust04/fr0mncdg/
3. Isolated scope
Demo: https://jsfiddle.net/masudcsesust04/9vo0339s/
4. Wraps other elements
Demo: https://jsfiddle.net/masudcsesust04/5wdm8xk0/
5. Communicative directive
Demo: https://jsfiddle.net/masudcsesust04/0s5uztax/
Any questions?
Thank you!

More Related Content

PPTX
Basics of angular directive (Part - 1)
PDF
Angular custom directives
PPTX
Building AngularJS Custom Directives
PPTX
BDOTNET AngularJs Directives - Uday
PDF
Custom AngularJS Directives
PPTX
Angular JS
PDF
AngularJS Basic Training
PPTX
Angular js
Basics of angular directive (Part - 1)
Angular custom directives
Building AngularJS Custom Directives
BDOTNET AngularJs Directives - Uday
Custom AngularJS Directives
Angular JS
AngularJS Basic Training
Angular js

What's hot (20)

PPTX
Getting Started with Angular JS
KEY
AngularJS for designers and developers
PPTX
Understanding angular js
PPTX
Angular js 1.0-fundamentals
PPTX
Introduction to AngularJs
PPTX
Angular Js Basics
PPTX
The AngularJS way
PPTX
Angular js architecture (v1.4.8)
PPTX
Angular JS - Introduction
PPT
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
PPTX
Modular javascript
PPTX
Angular js
PPTX
Angular js
PPTX
Angular Presentation
PPTX
Practical AngularJS
PDF
Advanced Tips & Tricks for using Angular JS
PPTX
Introduction to Angularjs
PPTX
Angular js
PPTX
Angular js
PPTX
Why angular js Framework
Getting Started with Angular JS
AngularJS for designers and developers
Understanding angular js
Angular js 1.0-fundamentals
Introduction to AngularJs
Angular Js Basics
The AngularJS way
Angular js architecture (v1.4.8)
Angular JS - Introduction
Dynamic Application Development by NodeJS ,AngularJS with OrientDB
Modular javascript
Angular js
Angular js
Angular Presentation
Practical AngularJS
Advanced Tips & Tricks for using Angular JS
Introduction to Angularjs
Angular js
Angular js
Why angular js Framework
Ad

Viewers also liked (20)

PDF
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
PDF
AngularJS Custom Directives
PPTX
Building an End-to-End AngularJS Application
PPTX
Getting Started Building Windows 8 HTML/JavaScript Metro Apps
PDF
Angular promises and http
PPTX
Angular Deep Directive
PPT
Coffee@DBG - Exploring Angular JS
PPTX
AngularJs presentation
PDF
AngularJS - TechTalk 3/2/2014
PPTX
AngularJS in 60ish Minutes
PPT
Using jQuery Templates
PPTX
JavaScript Patterns to Cleanup your Code
PDF
Angular Promises and Advanced Routing
PPTX
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
PPTX
Step by Step - AngularJS
PPTX
Development Trends - What's New in the World of Web Development
PPTX
Integrating Security Roles into Microsoft Silverlight Applications
PPTX
Planificación y organización de dominio
PPTX
Spa Architecture with Durandal and Friends
PDF
AngularJS Best Practices
Building Custom AngularJS Directives - A Step-by-Step Guide - Dan Wahlin | Fa...
AngularJS Custom Directives
Building an End-to-End AngularJS Application
Getting Started Building Windows 8 HTML/JavaScript Metro Apps
Angular promises and http
Angular Deep Directive
Coffee@DBG - Exploring Angular JS
AngularJs presentation
AngularJS - TechTalk 3/2/2014
AngularJS in 60ish Minutes
Using jQuery Templates
JavaScript Patterns to Cleanup your Code
Angular Promises and Advanced Routing
Building the an End-to-End ASP.NET MVC 4, Entity Framework, HTML5, jQuery app...
Step by Step - AngularJS
Development Trends - What's New in the World of Web Development
Integrating Security Roles into Microsoft Silverlight Applications
Planificación y organización de dominio
Spa Architecture with Durandal and Friends
AngularJS Best Practices
Ad

Similar to AngularJS custom directive (20)

PPTX
Directives
PDF
Angular directives and pipes
PPTX
Angular Directives
PPTX
AngularJs Workshop SDP December 28th 2014
PDF
Angular Directives from Scratch
PPTX
angularJs Workshop
PDF
Intro to Angular.JS Directives
DOCX
Directives
PDF
Angular 4 Directives | Angular 4 Tutorial For Beginners | Angular 4 Directive...
PPTX
AngularJS - GrapeCity Echo Tokyo
PPTX
Learning AngularJS - Complete coverage of AngularJS features and concepts
PDF
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
PPTX
Agular 2 - 6 Directives
PPTX
AIT SEMINAR.pptx
PPTX
Angular Directive.pptx
PPTX
AngularJS
PDF
Angular.js Primer in Aalto University
PPTX
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
PPTX
AngularJS
Directives
Angular directives and pipes
Angular Directives
AngularJs Workshop SDP December 28th 2014
Angular Directives from Scratch
angularJs Workshop
Intro to Angular.JS Directives
Directives
Angular 4 Directives | Angular 4 Tutorial For Beginners | Angular 4 Directive...
AngularJS - GrapeCity Echo Tokyo
Learning AngularJS - Complete coverage of AngularJS features and concepts
Angular Directives | Angular 2 Custom Directives | Angular Tutorial | Angular...
Agular 2 - 6 Directives
AIT SEMINAR.pptx
Angular Directive.pptx
AngularJS
Angular.js Primer in Aalto University
Angular training - Day 3 - custom directives, $http, $resource, setup with ye...
AngularJS

More from Nascenia IT (20)

PPTX
Exploring DeepSeek A Hands-On Dive & How to Adapt the AI Surge.pptx
PPTX
AI Tools for Productivity: Exploring Prompt Engineering and Key Features
PDF
Introduction to basic data analytics tools
PPTX
Communication workshop in nascenia
PPTX
The Art of Statistical Deception
PDF
করোনায় কী করি!
PPTX
GDPR compliance expectations from the development team
PPTX
Writing Clean Code
PPTX
History & Introduction of Neural Network and use of it in Computer Vision
PPTX
Ruby on Rails: Coding Guideline
PPTX
iphone 11 new features
PPTX
Software quality assurance and cyber security
PPTX
Job Market Scenario For Freshers
PPTX
Modern Frontend Technologies (BEM, Retina)
PPTX
CSS for Developers
PPTX
Big commerce app development
PPTX
Integrating QuickBooks Desktop with Rails Application
PPTX
Shopify
PPTX
TypeScript: Basic Features and Compilation Guide
PPTX
Clean code
Exploring DeepSeek A Hands-On Dive & How to Adapt the AI Surge.pptx
AI Tools for Productivity: Exploring Prompt Engineering and Key Features
Introduction to basic data analytics tools
Communication workshop in nascenia
The Art of Statistical Deception
করোনায় কী করি!
GDPR compliance expectations from the development team
Writing Clean Code
History & Introduction of Neural Network and use of it in Computer Vision
Ruby on Rails: Coding Guideline
iphone 11 new features
Software quality assurance and cyber security
Job Market Scenario For Freshers
Modern Frontend Technologies (BEM, Retina)
CSS for Developers
Big commerce app development
Integrating QuickBooks Desktop with Rails Application
Shopify
TypeScript: Basic Features and Compilation Guide
Clean code

Recently uploaded (20)

PDF
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Modernizing your data center with Dell and AMD
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
AI And Its Effect On The Evolving IT Sector In Australia - Elevate
Dropbox Q2 2025 Financial Results & Investor Presentation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Modernizing your data center with Dell and AMD
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Advanced methodologies resolving dimensionality complications for autism neur...
Advanced Soft Computing BINUS July 2025.pdf
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Per capita expenditure prediction using model stacking based on satellite ima...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Chapter 3 Spatial Domain Image Processing.pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm

AngularJS custom directive

  • 2. Topics - What are directives? - Angular built in directives. - Types of directives. - Convention to write a custom directive. - Example of directive to start writing.
  • 3. What are Directives? Directives are markers on a DOM element (such as an attribute, element name, comment or CSS class) that tell AngularJS's HTML compiler ($compile) to attach a specified behavior to that DOM element or even transform the DOM element and its children.
  • 4. AngularJS Built in Directives Angular comes with a set of these built-in directives like ngBind, ngModel, and ngClass etc.
  • 5. Conventions to Write a Directive - Case-sensitive (camelCase) normalized name(e.g. ngModel.) - HTML is case-insensitive, so directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (e.g. ng-model) - Strip x- and data- from the front of the element/attributes - Convert the :, -, or _-delimited name to camelCase
  • 6. Directive in HTML Template <div ng-controller="Controller"> Hello <input ng-model='name'> <hr/> <span ng-bind="name"></span> <br/> <span ng:bind="name"></span> <br/> <span ng_bind="name"></span> <br/> <span data-ng-bind="name"></span> <br/> <span x-ng-bind="name"></span> <br/> </div>
  • 7. Best Practice Prefer using the dash-delimited format (e.g. ng- bind for ngBind). If you want to use an HTML validating tool, you can instead use the data- prefixed version (e.g. data-ng-bind for ngBind). The other forms shown above are accepted for legacy reasons but we advise you to avoid them.
  • 8. Directive Types 1. Element names (HTML Tag) 2. Attributes 3. Class names and 4. Comments
  • 9. Example of Directive Types <my-dir></my-dir> <span my-dir="exp"></span> <!-- directive: my-dir exp --> <span class="my-dir: exp;"></span> Best Practice: Prefer using directives via tag name and attributes over comment and class names. Doing so generally makes it easier to determine what directives a given element matches.
  • 10. Instruction about Directives 1. Register a directive, use the module.directive 2. Directive name followed by a factory function 3. To avoid collisions with some future standard, it's best to prefix your own directive names. For instance, if you created a <carousel> directive, it would be problematic if HTML7 introduced the same element. A two or three letter prefix (e.g. btfCarousel) works well. 4. Do not prefix your own directives with ng
  • 11. Writing Some Custom Directive 1. Replace static content Demo: https://jsfiddle.net/masudcsesust04/nj25aznt/ 2. Restricted directive Demo: https://jsfiddle.net/masudcsesust04/fr0mncdg/ 3. Isolated scope Demo: https://jsfiddle.net/masudcsesust04/9vo0339s/ 4. Wraps other elements Demo: https://jsfiddle.net/masudcsesust04/5wdm8xk0/ 5. Communicative directive Demo: https://jsfiddle.net/masudcsesust04/0s5uztax/