SlideShare une entreprise Scribd logo
1  sur  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:

Contenu connexe

En vedette

Communique de presse sixieme journee autoimmunite
Communique de presse sixieme journee autoimmuniteCommunique de presse sixieme journee autoimmunite
Communique de presse sixieme journee autoimmuniteKhadija Moussayer
 
2013session2 1
2013session2 12013session2 1
2013session2 1acvq
 
Diapositivas tercera secuencia daniel
Diapositivas tercera secuencia danielDiapositivas tercera secuencia daniel
Diapositivas tercera secuencia danielMelissa Medina
 
My Diplomas
My DiplomasMy Diplomas
My Diplomasbdarquea
 
Yaritza fisicaparamelissa
Yaritza fisicaparamelissaYaritza fisicaparamelissa
Yaritza fisicaparamelissaMelissa Medina
 
Reflexion de fotos
Reflexion de fotosReflexion de fotos
Reflexion de fotosPottola
 
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 & TechniquesRuss U
 
Mahi arabic holiday homework
Mahi arabic holiday homeworkMahi arabic holiday homework
Mahi arabic holiday homeworkRahul 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 BlogsRCha
 
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, & CultureRuss U
 
IPv6 at Comcast, PTC17
IPv6 at Comcast, PTC17IPv6 at Comcast, PTC17
IPv6 at Comcast, PTC17APNIC
 

En vedette (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
 

Similaire à 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
 

Dernier

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 

Dernier (20)

call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 

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: