A directive
allows you to extend the HTML vocabulary in a declarative fashion for building web components. The ng-app
attribute is a directive, so is ng-controller
and all of the ng- prefixed attributes
. Directives can be attributes
, tags
or even class
names
, comments
.
How directives are born (compilation
and instantiation
)
Compile: We’ll use the compile
function to both manipulate
the DOM before it’s rendered and return a link
function (that will handle the linking for us). This also is the place to put any methods that need to be shared around with all of the instances
of this directive.
link: We’ll use the link
function to register all listeners on a specific DOM element (that’s cloned from the template) and set up our bindings to the page.
If set in the compile()
function they would only have been set once (which is often what you want). If set in the link()
function they would be set every time the HTML element is bound to data in the
object.
<div ng-repeat="i in [0,1,2]">
<simple>
<div>Inner content</div>
</simple>
</div>
app.directive("simple", function(){
return {
restrict: "EA",
transclude:true,
template:"<div>{{label}}<div ng-transclude></div></div>",
compile: function(element, attributes){
return {
pre: function(scope, element, attributes, controller, transcludeFn){
},
post: function(scope, element, attributes, controller, transcludeFn){
}
}
},
controller: function($scope){
}
};
});
Compile
function returns the pre
and post
link function. In the pre link function we have the instance template and also the scope from the controller
, but yet the template is not bound to scope and still don't have transcluded content.
Post
link function is where post link is the last function to execute. Now the transclusion
is complete, the template is linked to a scope
, and the view will update with data bound values after the next digest cycle
. The link
option is just a shortcut to setting up a post-link
function.
controller: The directive controller can be passed to another directive linking/compiling phase. It can be injected into other directices as a mean to use in inter-directive communication.
You have to specify the name of the directive to be required – It should be bound to same element or its parent. The name can be prefixed with:
? – Will not raise any error if a mentioned directive does not exist.
^ – Will look for the directive on parent elements, if not available on the same element.
Use square bracket [‘directive1′, ‘directive2′, ‘directive3′]
to require multiple directives controller.
var app = angular.module('app', []);
app.controller('MainCtrl', function($scope, $element) {
});
app.directive('parentDirective', function() {
return {
restrict: 'E',
template: '<child-directive></child-directive>',
controller: function($scope, $element){
this.variable = "Hi Vinothbabu"
}
}
});
app.directive('childDirective', function() {
return {
restrict: 'E',
template: '<h1>I am child</h1>',
replace: true,
require: '^parentDirective',
link: function($scope, $element, attr, parentDirectCtrl){
//you now have access to parentDirectCtrl.variable
}
}
});