It is possible to run javascript in a directive before returning anything,
as well as in a directive's compile step before returning anything:
angular.module('foo').directive('fooDirective', [function(){
console.debug('before return');
return {
restrict: 'E',
controller: function($scope){
console.debug('controller');
},
compile: function(scope, elem){
console.debug('compile');
return {
pre: function(scope,elem, attr){
console.debug('pre');
},
post: function(scope,elem,attr){
console.debug('post');
}
}
}
}
}]);
<body ng-app="foo">
<foo-directive></foo-directive>
<foo-directive></foo-directive>
</body>
This produces the following console log order:
before return
compile
controller
pre
post
before return
compile
compile
controller
pre
post
controller
pre
post
I have several questions about this:
1) Why would I ever want to run code before returning the actual directive
object? What would be a usecase?
2) Why would I ever want to run code before returning the pre/post link
functions? How is the prelink step different from the compile step? What is
a use case?
3) Why does compile run twice in succession when there is two items, while
everything else runs iteratively in the same order irrelevantly of number
of elements?
4) Why does everything except "before return" run three times? Shouldn't
everything only run twice?
5) (Corollary to 4 really) Why does before return only run twice?
Plunk: http://plnkr.co/edit/1JPYLcPlMerXlwr0GnND?p=preview
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.