Beruflich Dokumente
Kultur Dokumente
Custom Directives
Day #3
BY
George
Agenda
Possible way to Call Directives
Directive Types
Element directives
Attribute directives
CSS class directives
Comment directives
AngularJS even gives you other options for invoking directives with different name
prefixes
As an attribute
An attribute directive is activated when
AngularJS finds a matching HTML
element attribute
As a class
A CSS class directive is activated when
AngularJS finds a matching CSS Class
As an element
An element directive is activated when
AngularJS finds a matching HTML
element in the HTML template
As a comment
A comment directive is activated when
AngularJS finds a matching HTML
comment.
When you call this function you can register a new directive.
Restrict option
There are four different ways to invoke a directive, so there are four valid options for restrict:
Template
This template is an inline template where we are specifying the html that will be
appended
This is particularly useful when we want to share directives across apps and you only
want to pass a single file around.
TemplateUrl
If you prefer to load a template over ajax, you
can specify the templateUrl option, which will
use ajax to pull the template.
link function is acitve always - whenever the scope changes the link
function is refreshed
The $compile() function will return a linking function that wraps all of
the containing DOM elements directives' linking functions.
Well use the link function to register all listeners on a specific DOM
element
Require option
Well enforce that we need this dependency by setting the require option:
Now, if we invoke the directive on the page without the ng-model directive, our browser will complain
and throw an error. To invoke our directive now, we simply have to add the ng-model directive:
Notice, in the require option, we prefixed the controller with a ^. AngularJS gives you two options in the
require option about how to declare requirements with a prefixed character:
Scope
Just like in every other part of AngularJS DOM control, directives can be given their
own scopes.
AngularJS gives you the ability to isolate the scope of the directive from the rest of
the page using the scope option.
The scope option can take one of two different options. It can be set to true (its
false by default).
When scope is set to false, the controller Ctrl1 and directive are using the same
scope object. This means any changes to the controller or directive will be in sync.
fiddle : http://jsfiddle.net/shidhincr/eyNYw/4/light/
When scope is set to true, AngularJS will create a new scope by inheriting parent scope
fiddle : http://jsfiddle.net/shidhincr/q3kex/3/light/
This time, there will be a new scope created for the directive, but it will not be inherited
from the parent scope. This new scope also known as Isolated scope because it is
completely detached from its parent scope.
fiddle: http://jsfiddle.net/shidhincr/q3kex/4/light/
can we pass some values from the parent scope to the directives now?
Yes ! Not only that, we might need to handle situations like invoking callbacks in parent
scope, two-way binding between parent & directives scope ..etc
fiddle : http://jsfiddle.net/shidhincr/pJLT8/10/light/
Bi-directional binding
Directive Controller
Transclude option
http://jsfiddle.net/anglee/StnqM/
Replace option
replace: true means that the content of
the directive template will replace the
element that the directive is declared
on.
When replace: true, the template or
templateUrl must be required.
http://jsfiddle.net/cw773/
Assigenments