SlideShare a Scribd company logo
1 of 12
DIRECTIVES
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.
DIRECTIVES
• Angular comes with a set of these directives
built-in, like ngBind, ngModel, and ngClass.
• Much like you create controllers and services, you
can create your own directives for Angular to use.
• When Angular bootstraps your application, the
HTML compiler traverses the DOM matching
directives against the DOM elements.
• $compile can match directives based on
element names, attributes, class names, as
well as comments.
MATCHING DIRECTIVES
• Angular normalizes an element's tag and attribute
name to determine which elements match which
directives.
• We typically refer to directives by their case-sensitive
camelCase normalized name (e.g. ngModel).
• However, since HTML is case-insensitive, we refer to
directives in the DOM by lower-case forms, typically
using dash-delimited attributes on DOM elements
(e.g. ng-model).
• The normalization process is as follows:
• Strip x- and data- from the front of the element/attributes.
• Convert the :, -, or _-delimited name to camelCase.
BUILT-IN DIRECTIVES
CREATING DIRECTIVES
• Directives are registered on modules.
• To register a directive, you use the
module.directive API.
• module.directive takes the normalized directive
name followed by a factory function.
• This factory function should return an object with
the different options to tell $compile how the
directive should behave when matched.
• The factory function is invoked only once when
the compiler matches the directive for the first
time.
BEST PRACTICE
• In order 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.
• Similarly, do not prefix your own directives with
ng or they might conflict with directives included
in a future version of Angular.
CREATING DIRECTIVES
CREATING DIRECTIVES
CREATING DIRECTIVES
• templateUrl can also be a function which returns the URL of
an HTML template to be loaded and used for the directive.
• Angular will call the templateUrl function with two
parameters: the element that the directive was called on,
and an attr object associated with that element.
CREATING DIRECTIVES
• When you create a directive, it is restricted to
attribute and elements only by default. In order to
create directives that are triggered by class name,
you need to use the restrict option.
• The restrict option is typically set to:
• 'A' - only matches attribute name
• 'E' - only matches element name
• 'C' - only matches class name
• These restrictions can all be combined as needed:
• 'AEC' - matches either attribute or element or class
name
ISOLATING THE SCOPE OF A
DIRECTIVE
• Our myCustomer directive above is great, but it
has a fatal flaw. We can only use it once within a
given scope.
• In its current implementation, we'd need to
create a different controller each time in order to
re-use such a directive:

More Related Content

Viewers also liked

2013session2 1
2013session2 12013session2 1
2013session2 1
acvq
 
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & TechniquesCrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
Russ U
 
Mahi arabic holiday homework
Mahi arabic holiday homeworkMahi arabic holiday homework
Mahi arabic holiday homework
Rahul Vohra
 
Maladies rares et maladies auto-immunes et systémiques journée de l'auto i...
Maladies rares  et maladies auto-immunes et systémiques  journée de l'auto i...Maladies rares  et maladies auto-immunes et systémiques  journée de l'auto i...
Maladies rares et maladies auto-immunes et systémiques journée de l'auto i...
Khadija Moussayer
 
Sagt07 Join Online Revolution Example Blogs
Sagt07 Join Online Revolution Example BlogsSagt07 Join Online Revolution Example Blogs
Sagt07 Join Online Revolution Example Blogs
RCha
 
3 Cs of Design: Charters, Critique, & Culture
3 Cs of Design: Charters, Critique, & Culture3 Cs of Design: Charters, Critique, & Culture
3 Cs of Design: Charters, Critique, & Culture
Russ U
 

Viewers also liked (20)

Communique de presse sixieme journee autoimmunite
Communique de presse sixieme journee autoimmuniteCommunique de presse sixieme journee autoimmunite
Communique de presse sixieme journee autoimmunite
 
пасха
пасхапасха
пасха
 
Techm certi
Techm certiTechm certi
Techm certi
 
2013session2 1
2013session2 12013session2 1
2013session2 1
 
Diapositivas tercera secuencia daniel
Diapositivas tercera secuencia danielDiapositivas tercera secuencia daniel
Diapositivas tercera secuencia daniel
 
My Diplomas
My DiplomasMy Diplomas
My Diplomas
 
Yaritza fisicaparamelissa
Yaritza fisicaparamelissaYaritza fisicaparamelissa
Yaritza fisicaparamelissa
 
Reflexion de fotos
Reflexion de fotosReflexion de fotos
Reflexion de fotos
 
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & TechniquesCrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
CrankyTalk Workshop for New Speakers, Chicago - Presentation Tips & Techniques
 
Mahi arabic holiday homework
Mahi arabic holiday homeworkMahi arabic holiday homework
Mahi arabic holiday homework
 
Latvian muffins
Latvian muffinsLatvian muffins
Latvian muffins
 
Manual excel revelado 2013
Manual excel revelado 2013Manual excel revelado 2013
Manual excel revelado 2013
 
Maladies rares et maladies auto-immunes et systémiques journée de l'auto i...
Maladies rares  et maladies auto-immunes et systémiques  journée de l'auto i...Maladies rares  et maladies auto-immunes et systémiques  journée de l'auto i...
Maladies rares et maladies auto-immunes et systémiques journée de l'auto i...
 
estadistica_descriptiva
estadistica_descriptivaestadistica_descriptiva
estadistica_descriptiva
 
Lost Latinos
Lost LatinosLost Latinos
Lost Latinos
 
Legislación marítima
Legislación marítima Legislación marítima
Legislación marítima
 
Estadistica
EstadisticaEstadistica
Estadistica
 
Sagt07 Join Online Revolution Example Blogs
Sagt07 Join Online Revolution Example BlogsSagt07 Join Online Revolution Example Blogs
Sagt07 Join Online Revolution Example Blogs
 
3 Cs of Design: Charters, Critique, & Culture
3 Cs of Design: Charters, Critique, & Culture3 Cs of Design: Charters, Critique, & Culture
3 Cs of Design: Charters, Critique, & Culture
 
IPv6 at Comcast, PTC17
IPv6 at Comcast, PTC17IPv6 at Comcast, PTC17
IPv6 at Comcast, PTC17
 

Similar to Directives

BDOTNET AngularJs Directives - Uday
BDOTNET AngularJs Directives - UdayBDOTNET AngularJs Directives - Uday
BDOTNET AngularJs Directives - Uday
Udaya Kumar
 

Similar to Directives (20)

AngularJS custom directive
AngularJS custom directiveAngularJS custom directive
AngularJS custom directive
 
BDOTNET AngularJs Directives - Uday
BDOTNET AngularJs Directives - UdayBDOTNET AngularJs Directives - Uday
BDOTNET AngularJs Directives - Uday
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
Angular js 1.3 presentation for fed nov 2014
Angular js 1.3 presentation for fed   nov 2014Angular js 1.3 presentation for fed   nov 2014
Angular js 1.3 presentation for fed nov 2014
 
Angular Basics.pptx
Angular Basics.pptxAngular Basics.pptx
Angular Basics.pptx
 
Angular Directive.pptx
Angular Directive.pptxAngular Directive.pptx
Angular Directive.pptx
 
Angular js
Angular jsAngular js
Angular js
 
Angular JS Indtrodution
Angular JS IndtrodutionAngular JS Indtrodution
Angular JS Indtrodution
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angular Js Get Started - Complete Course
Angular Js Get Started - Complete CourseAngular Js Get Started - Complete Course
Angular Js Get Started - Complete Course
 
Angular
AngularAngular
Angular
 
Angular
AngularAngular
Angular
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS
AngularJSAngularJS
AngularJS
 
Angular js
Angular jsAngular js
Angular js
 
Angular js
Angular jsAngular js
Angular js
 
AngularJS.pptx
AngularJS.pptxAngularJS.pptx
AngularJS.pptx
 
Mvc
MvcMvc
Mvc
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
 
AngularJS Workshop
AngularJS WorkshopAngularJS Workshop
AngularJS Workshop
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - KanchanaWSO2Con2024 - Hello Choreo Presentation - Kanchana
WSO2Con2024 - Hello Choreo Presentation - Kanchana
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Directives

  • 2. 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.
  • 3. DIRECTIVES • Angular comes with a set of these directives built-in, like ngBind, ngModel, and ngClass. • Much like you create controllers and services, you can create your own directives for Angular to use. • When Angular bootstraps your application, the HTML compiler traverses the DOM matching directives against the DOM elements. • $compile can match directives based on element names, attributes, class names, as well as comments.
  • 4. MATCHING DIRECTIVES • Angular normalizes an element's tag and attribute name to determine which elements match which directives. • We typically refer to directives by their case-sensitive camelCase normalized name (e.g. ngModel). • However, since HTML is case-insensitive, we refer to directives in the DOM by lower-case forms, typically using dash-delimited attributes on DOM elements (e.g. ng-model). • The normalization process is as follows: • Strip x- and data- from the front of the element/attributes. • Convert the :, -, or _-delimited name to camelCase.
  • 6. CREATING DIRECTIVES • Directives are registered on modules. • To register a directive, you use the module.directive API. • module.directive takes the normalized directive name followed by a factory function. • This factory function should return an object with the different options to tell $compile how the directive should behave when matched. • The factory function is invoked only once when the compiler matches the directive for the first time.
  • 7. BEST PRACTICE • In order 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. • Similarly, do not prefix your own directives with ng or they might conflict with directives included in a future version of Angular.
  • 10. CREATING DIRECTIVES • templateUrl can also be a function which returns the URL of an HTML template to be loaded and used for the directive. • Angular will call the templateUrl function with two parameters: the element that the directive was called on, and an attr object associated with that element.
  • 11. CREATING DIRECTIVES • When you create a directive, it is restricted to attribute and elements only by default. In order to create directives that are triggered by class name, you need to use the restrict option. • The restrict option is typically set to: • 'A' - only matches attribute name • 'E' - only matches element name • 'C' - only matches class name • These restrictions can all be combined as needed: • 'AEC' - matches either attribute or element or class name
  • 12. ISOLATING THE SCOPE OF A DIRECTIVE • Our myCustomer directive above is great, but it has a fatal flaw. We can only use it once within a given scope. • In its current implementation, we'd need to create a different controller each time in order to re-use such a directive: