SlideShare una empresa de Scribd logo
1 de 27
markers on a DOM element (attribute, element name, 
comment or CSS class); 
"... a way to tech HTML new tricks."; 
anything (within your app) ; 
a function that's attached to a DOM element: 
a whole execution environment; 
a micro-application; 
<!-- Built in directive --> 
<div ng-repeat="item in items"> </div> 
<!-- Custom directive --> 
<my-directive> </my-directive> 
<!-- OR --> 
<div my-directive> </div>
event handling; 
behavior management; 
template pre-processing and insertion; 
data-binding; 
UI Widgets; 
much more.
1. Declarative, Model-Driven Behaviour 
2. Modularity, Reusability across contexts 
3. Keep it local 
<!-- Imperative Way --> 
<button id="1" class="B C"></button> 
<!-- V.s. Declarative way--> 
<button my-action-handler></button>
Directive Names Angular uses a convention borrowed 
from other JS projects: names in HTML are hyphenated 
<my-directive> </my-directive> 
.directive('myDirective', function(){ 
}); 
while identifiers in the JS are camel-cased
angular 
.module('myApp', []) 
.directive('myDirective', function(){ 
return{ 
link: function(scope, lement, attrs, ctrl){ 
element.bind("mouseenter", function(){ 
element.css({color: 'red'}); 
}); 
}, 
require: '', 
controller: ['$scope', '$element', function($scope, $element){}], 
scope: false, 
transclude: false, 
restrict: 'E', 
template: "<div>Hello, World!</div>" 
}; 
}); 
Everything but is optional.
The params are supplied as args not injected: 
scope associated with the directive 
DOM element wrapped by jQLite 
an object containing the html attributes defined 
on the element 
Reference to controller object
Templates can be stored as strings on the 
property; 
They can also be loaded from a file, using 
property
Directives can be restricted to a specific context, so 
the accidentally usage can be avoided: 
- Element 
- Attribute 
- Class 
Can be used as a single string: ' '; 
Defaults to ' '
We have the option, in directives, of using either: 
the same scope (scope from the environment that 
directive lives) 
a child scope that inherits from current scope 
a new isolate scope 
.directive('myDirective', function(){ 
return { 
//scope: false // default, use local scope 
//scope: true // create a child of local scope 
//Create a new isolate scope 
scope: { 
} 
} 
});
Angular provides ways to bind the value of properties in 
isolate scope to attributes on the element, using special 
operators: 
- Local scope property 
- Bi-directional binding 
- Parent execution binding 
scope: { 
local1: '@attr1', 
local2: '=attr1', 
local3: '&attr3' 
}
Plunker link
Transclude allow to pass in an entire template, 
including its scope, to a directive. 
In order for scope to be passed in, the scope option 
must be isolated, , or set to . 
<div sidebox title="Links"> 
<ul> 
<li>First link</li> 
<li>Second link</li> 
</ul> 
</div> 
<script type="text/ng-template" id="directive.template.html"> 
{{ title }} 
angular.module('myApp', []) 
.directive('sidebox', function() { 
return { 
restrict: 'EA', 
scope: { 
title: '@' 
}, 
transclude: true, 
templateUrl: 'directive.template.html' 
});
The main use case for a controller is when we want to provide reusable 
behavior between directives 
As the link function is only available inside the current directive, any 
behavior defined within is not shareable 
Because a directive can require the controller of another directive, 
controllers are a good choice for placing actions that will be used by 
multiple directives.
The option can be set to a string or an array of 
strings. 
The string(s) contain the name of another directive. 
is used to provide the controller of the required 
directive as the fourth parameter of the current directive’s 
linking function 
The string(s) provided to the require option may optionally be prefixed with the 
following options: 
- If the required controller is not found on the directive provided, pass 
look upwards on its parent chain for the controller in the require option 
optionally require the controller and look up the parent chain for the 
controller
<level-one> 
<level-two> 
<level-three> 
Hello {{name}} 
</level-three> 
</level-two> 
</level-one> 
function createDirective(name){ 
return function(){ 
return { 
restrict: 'E', 
compile: function(tElem, tAttrs){ 
console.log(name + ': compile => ' + tElem.html()); 
return { 
pre: function(scope, iElem, iAttrs){ 
console.log(name + ': pre link => ' + iElem.html()); 
}, 
post: function(scope, iElem, iAttrs){ 
console.log(name + ': post link => ' + iElem.html()); 
} 
} 
} 
} 
} 
} 
angular.module('myModule', []) 
.directive('levelOne', createDirective('levelOne')) 
.directive('levelTwo', createDirective('levelTwo')) 
.directive('levelThree', createDirective('levelThree')); 
let AngularJS process three nested directives where each directive has its own 
, and function that logs a line to the console so we can identify them.
1. Angular start to process the DOM when it detects that the DOM is ready; 
2. it bumps into the element and knows from its directive definition that 
some action needs to be performed: 
1. Because a function is defined in the directive definition 
object, it is called and the element's DOM is passed as an argument to the 
function; 
2. Once the compile function of the directive has run, Angular 
recursively traverses deeper into the DOM and repeats the same compilation 
step for the and <level-three> elements.
1. After Angular travels down the DOM and has run all the 
functions, it traverses back up again and runs all associated 
functions. 
2. The DOM is now traversed in the opposite direction and thus the 
functions are called in reverse order. 
3. This reverse order guarantees that the functions of all 
child elements have run by the time the function of the 
parent element is run. 
4. Once Angular has called the function of a directive, it 
creates an instance element of the template element and 
provides a scope for the instance. 
5. So by the time the linking occurs, the instance element and scope 
are already available and they are passed by Angular as 
arguments to the function.
1. Angular provides a way to run code before any of the child 
element's functions have run; 
2. Angular calls in the original order; 
3. A function of an element is guaranteed to run before any 
or function of any of its child elements.
Use only to change the original DOM (template element) before 
Angular creates an instance of it and before a scope is created. 
Use to implement logic that runs when Angular has already 
compiled the child elements, but before any of the child 
element's and functions have been called. 
Use to execute logic, knowing that all child elements have been 
compiled and all and post-link functions of child 
elements have been executed.
The nitty-gritty of compile and link functions inside AngularJS directives
Plunker link
Angular Api Reference 
ng-book 
The nitty-gritty of compile and link functions inside 
AngularJS directives

Más contenido relacionado

La actualidad más candente

AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS DirectivesEyal Vardi
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developersKai Koenig
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJSBrajesh Yadav
 
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Christian Lilley
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS ArchitectureEyal Vardi
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.Yan Yankowski
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar SimovićJS Belgrade
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS InternalEyal Vardi
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionVisual Engineering
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSVisual Engineering
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsVisual Engineering
 
Opinionated AngularJS
Opinionated AngularJSOpinionated AngularJS
Opinionated AngularJSprabhutech
 
Modular javascript
Modular javascriptModular javascript
Modular javascriptZain Shaikh
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsEPAM Systems
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationDan Wahlin
 

La actualidad más candente (20)

AngularJS Directives
AngularJS DirectivesAngularJS Directives
AngularJS Directives
 
AngularJS for designers and developers
AngularJS for designers and developersAngularJS for designers and developers
AngularJS for designers and developers
 
Angular js
Angular jsAngular js
Angular js
 
AngularJs
AngularJsAngularJs
AngularJs
 
AngularJS Basics with Example
AngularJS Basics with ExampleAngularJS Basics with Example
AngularJS Basics with Example
 
Filters in AngularJS
Filters in AngularJSFilters in AngularJS
Filters in AngularJS
 
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
Scalable CSS You and Your Back-End Coders Can Love - @CSSConf Asia 2014
 
AngularJS Architecture
AngularJS ArchitectureAngularJS Architecture
AngularJS Architecture
 
AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.AngularJs $provide API internals & circular dependency problem.
AngularJs $provide API internals & circular dependency problem.
 
AngularJS.part1
AngularJS.part1AngularJS.part1
AngularJS.part1
 
Angular In Depth
Angular In DepthAngular In Depth
Angular In Depth
 
"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović"Angular.js Concepts in Depth" by Aleksandar Simović
"Angular.js Concepts in Depth" by Aleksandar Simović
 
AngularJS Internal
AngularJS InternalAngularJS Internal
AngularJS Internal
 
Workshop 19: ReactJS Introduction
Workshop 19: ReactJS IntroductionWorkshop 19: ReactJS Introduction
Workshop 19: ReactJS Introduction
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Workshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design PatternsWorkshop 2: JavaScript Design Patterns
Workshop 2: JavaScript Design Patterns
 
Opinionated AngularJS
Opinionated AngularJSOpinionated AngularJS
Opinionated AngularJS
 
Modular javascript
Modular javascriptModular javascript
Modular javascript
 
Jquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript BasicsJquery Complete Presentation along with Javascript Basics
Jquery Complete Presentation along with Javascript Basics
 
Building an End-to-End AngularJS Application
Building an End-to-End AngularJS ApplicationBuilding an End-to-End AngularJS Application
Building an End-to-End AngularJS Application
 

Destacado

AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injectionAlexe Bogdan
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in AngularAlexe Bogdan
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basicsRavindra K
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS introdizabl
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedStéphane Bégaudeau
 

Destacado (7)

AngularJS - dependency injection
AngularJS - dependency injectionAngularJS - dependency injection
AngularJS - dependency injection
 
Dependency Injection pattern in Angular
Dependency Injection pattern in AngularDependency Injection pattern in Angular
Dependency Injection pattern in Angular
 
Ajs ppt
Ajs pptAjs ppt
Ajs ppt
 
introduction to Angularjs basics
introduction to Angularjs basicsintroduction to Angularjs basics
introduction to Angularjs basics
 
AngularJS intro
AngularJS introAngularJS intro
AngularJS intro
 
AngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get startedAngularJS 101 - Everything you need to know to get started
AngularJS 101 - Everything you need to know to get started
 
Introduction to Angularjs
Introduction to AngularjsIntroduction to Angularjs
Introduction to Angularjs
 

Similar a Angular custom directives

AngularJS Custom Directives
AngularJS Custom DirectivesAngularJS Custom Directives
AngularJS Custom Directivesyprodev
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - DirectivesWebStackAcademy
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs WorkshopRan Wahle
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014Ran Wahle
 
Angular Presentation
Angular PresentationAngular Presentation
Angular PresentationAdam Moore
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesMarios Fakiolas
 
"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero Parviainen"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero ParviainenFwdays
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular IntermediateLinkMe Srl
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIVisual Engineering
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to AngularjsGaurav Agrawal
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Salesforce Developers
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2Knoldus Inc.
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development GuideNitin Giri
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxBOSC Tech Labs
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIEric Wise
 

Similar a Angular custom directives (20)

AngularJS Custom Directives
AngularJS Custom DirectivesAngularJS Custom Directives
AngularJS Custom Directives
 
Angular - Chapter 5 - Directives
 Angular - Chapter 5 - Directives Angular - Chapter 5 - Directives
Angular - Chapter 5 - Directives
 
angularJs Workshop
angularJs WorkshopangularJs Workshop
angularJs Workshop
 
AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014AngularJs Workshop SDP December 28th 2014
AngularJs Workshop SDP December 28th 2014
 
Directives
DirectivesDirectives
Directives
 
AngularJS in practice
AngularJS in practiceAngularJS in practice
AngularJS in practice
 
Angular2 and You
Angular2 and YouAngular2 and You
Angular2 and You
 
Angular Presentation
Angular PresentationAngular Presentation
Angular Presentation
 
Angular js-crash-course
Angular js-crash-courseAngular js-crash-course
Angular js-crash-course
 
Get rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directivesGet rid of controllers in angular 1.5.x start using component directives
Get rid of controllers in angular 1.5.x start using component directives
 
"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero Parviainen"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero Parviainen
 
Angular Intermediate
Angular IntermediateAngular Intermediate
Angular Intermediate
 
AngularJs Crash Course
AngularJs Crash CourseAngularJs Crash Course
AngularJs Crash Course
 
Workshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte IIIWorkshop 14: AngularJS Parte III
Workshop 14: AngularJS Parte III
 
Intoduction to Angularjs
Intoduction to AngularjsIntoduction to Angularjs
Intoduction to Angularjs
 
Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1Mastering the Lightning Framework - Part 1
Mastering the Lightning Framework - Part 1
 
Introduction to Angular 2
Introduction to Angular 2Introduction to Angular 2
Introduction to Angular 2
 
Angular workshop - Full Development Guide
Angular workshop - Full Development GuideAngular workshop - Full Development Guide
Angular workshop - Full Development Guide
 
React Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptxReact Hooks Best Practices in 2022.pptx
React Hooks Best Practices in 2022.pptx
 
AngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPIAngularJS Fundamentals + WebAPI
AngularJS Fundamentals + WebAPI
 

Más de Alexe Bogdan

Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and httpAlexe Bogdan
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & AngularAlexe Bogdan
 
HTML & JavaScript Introduction
HTML & JavaScript IntroductionHTML & JavaScript Introduction
HTML & JavaScript IntroductionAlexe Bogdan
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communicationAlexe Bogdan
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced RoutingAlexe Bogdan
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?Alexe Bogdan
 

Más de Alexe Bogdan (6)

Angular promises and http
Angular promises and httpAngular promises and http
Angular promises and http
 
Client Side MVC & Angular
Client Side MVC & AngularClient Side MVC & Angular
Client Side MVC & Angular
 
HTML & JavaScript Introduction
HTML & JavaScript IntroductionHTML & JavaScript Introduction
HTML & JavaScript Introduction
 
Angular server-side communication
Angular server-side communicationAngular server-side communication
Angular server-side communication
 
Angular Promises and Advanced Routing
Angular Promises and Advanced RoutingAngular Promises and Advanced Routing
Angular Promises and Advanced Routing
 
AngularJS - introduction & how it works?
AngularJS - introduction & how it works?AngularJS - introduction & how it works?
AngularJS - introduction & how it works?
 

Último

SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxNIMMANAGANTI RAMAKRISHNA
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxAndrieCagasanAkio
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxmibuzondetrabajo
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxMario
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119APNIC
 
Cybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesCybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesLumiverse Solutions Pvt Ltd
 

Último (9)

SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
ETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptxETHICAL HACKING dddddddddddddddfnandni.pptx
ETHICAL HACKING dddddddddddddddfnandni.pptx
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
TRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptxTRENDS Enabling and inhibiting dimensions.pptx
TRENDS Enabling and inhibiting dimensions.pptx
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
Unidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptxUnidad 4 – Redes de ordenadores (en inglés).pptx
Unidad 4 – Redes de ordenadores (en inglés).pptx
 
Company Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptxCompany Snapshot Theme for Business by Slidesgo.pptx
Company Snapshot Theme for Business by Slidesgo.pptx
 
IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119IP addressing and IPv6, presented by Paul Wilson at IETF 119
IP addressing and IPv6, presented by Paul Wilson at IETF 119
 
Cybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best PracticesCybersecurity Threats and Cybersecurity Best Practices
Cybersecurity Threats and Cybersecurity Best Practices
 

Angular custom directives

  • 1.
  • 2. markers on a DOM element (attribute, element name, comment or CSS class); "... a way to tech HTML new tricks."; anything (within your app) ; a function that's attached to a DOM element: a whole execution environment; a micro-application; <!-- Built in directive --> <div ng-repeat="item in items"> </div> <!-- Custom directive --> <my-directive> </my-directive> <!-- OR --> <div my-directive> </div>
  • 3. event handling; behavior management; template pre-processing and insertion; data-binding; UI Widgets; much more.
  • 4. 1. Declarative, Model-Driven Behaviour 2. Modularity, Reusability across contexts 3. Keep it local <!-- Imperative Way --> <button id="1" class="B C"></button> <!-- V.s. Declarative way--> <button my-action-handler></button>
  • 5. Directive Names Angular uses a convention borrowed from other JS projects: names in HTML are hyphenated <my-directive> </my-directive> .directive('myDirective', function(){ }); while identifiers in the JS are camel-cased
  • 6. angular .module('myApp', []) .directive('myDirective', function(){ return{ link: function(scope, lement, attrs, ctrl){ element.bind("mouseenter", function(){ element.css({color: 'red'}); }); }, require: '', controller: ['$scope', '$element', function($scope, $element){}], scope: false, transclude: false, restrict: 'E', template: "<div>Hello, World!</div>" }; }); Everything but is optional.
  • 7. The params are supplied as args not injected: scope associated with the directive DOM element wrapped by jQLite an object containing the html attributes defined on the element Reference to controller object
  • 8. Templates can be stored as strings on the property; They can also be loaded from a file, using property
  • 9. Directives can be restricted to a specific context, so the accidentally usage can be avoided: - Element - Attribute - Class Can be used as a single string: ' '; Defaults to ' '
  • 10. We have the option, in directives, of using either: the same scope (scope from the environment that directive lives) a child scope that inherits from current scope a new isolate scope .directive('myDirective', function(){ return { //scope: false // default, use local scope //scope: true // create a child of local scope //Create a new isolate scope scope: { } } });
  • 11. Angular provides ways to bind the value of properties in isolate scope to attributes on the element, using special operators: - Local scope property - Bi-directional binding - Parent execution binding scope: { local1: '@attr1', local2: '=attr1', local3: '&attr3' }
  • 13. Transclude allow to pass in an entire template, including its scope, to a directive. In order for scope to be passed in, the scope option must be isolated, , or set to . <div sidebox title="Links"> <ul> <li>First link</li> <li>Second link</li> </ul> </div> <script type="text/ng-template" id="directive.template.html"> {{ title }} angular.module('myApp', []) .directive('sidebox', function() { return { restrict: 'EA', scope: { title: '@' }, transclude: true, templateUrl: 'directive.template.html' });
  • 14. The main use case for a controller is when we want to provide reusable behavior between directives As the link function is only available inside the current directive, any behavior defined within is not shareable Because a directive can require the controller of another directive, controllers are a good choice for placing actions that will be used by multiple directives.
  • 15. The option can be set to a string or an array of strings. The string(s) contain the name of another directive. is used to provide the controller of the required directive as the fourth parameter of the current directive’s linking function The string(s) provided to the require option may optionally be prefixed with the following options: - If the required controller is not found on the directive provided, pass look upwards on its parent chain for the controller in the require option optionally require the controller and look up the parent chain for the controller
  • 16.
  • 17. <level-one> <level-two> <level-three> Hello {{name}} </level-three> </level-two> </level-one> function createDirective(name){ return function(){ return { restrict: 'E', compile: function(tElem, tAttrs){ console.log(name + ': compile => ' + tElem.html()); return { pre: function(scope, iElem, iAttrs){ console.log(name + ': pre link => ' + iElem.html()); }, post: function(scope, iElem, iAttrs){ console.log(name + ': post link => ' + iElem.html()); } } } } } } angular.module('myModule', []) .directive('levelOne', createDirective('levelOne')) .directive('levelTwo', createDirective('levelTwo')) .directive('levelThree', createDirective('levelThree')); let AngularJS process three nested directives where each directive has its own , and function that logs a line to the console so we can identify them.
  • 18.
  • 19.
  • 20. 1. Angular start to process the DOM when it detects that the DOM is ready; 2. it bumps into the element and knows from its directive definition that some action needs to be performed: 1. Because a function is defined in the directive definition object, it is called and the element's DOM is passed as an argument to the function; 2. Once the compile function of the directive has run, Angular recursively traverses deeper into the DOM and repeats the same compilation step for the and <level-three> elements.
  • 21. 1. After Angular travels down the DOM and has run all the functions, it traverses back up again and runs all associated functions. 2. The DOM is now traversed in the opposite direction and thus the functions are called in reverse order. 3. This reverse order guarantees that the functions of all child elements have run by the time the function of the parent element is run. 4. Once Angular has called the function of a directive, it creates an instance element of the template element and provides a scope for the instance. 5. So by the time the linking occurs, the instance element and scope are already available and they are passed by Angular as arguments to the function.
  • 22.
  • 23. 1. Angular provides a way to run code before any of the child element's functions have run; 2. Angular calls in the original order; 3. A function of an element is guaranteed to run before any or function of any of its child elements.
  • 24. Use only to change the original DOM (template element) before Angular creates an instance of it and before a scope is created. Use to implement logic that runs when Angular has already compiled the child elements, but before any of the child element's and functions have been called. Use to execute logic, knowing that all child elements have been compiled and all and post-link functions of child elements have been executed.
  • 25. The nitty-gritty of compile and link functions inside AngularJS directives
  • 27. Angular Api Reference ng-book The nitty-gritty of compile and link functions inside AngularJS directives