functionality of angular directive before return and compile chain
-
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 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? Plunk: http://plnkr.co/edit/1JPYLcPlMerXlwr0GnND?p=preview
-
Answer:
You would want to do this so you can have some private method definitions that only your object can use. You would want to do this if you were utilizing some service precompile to get the data for the directive, and post compile to use the data and do something with it. No idea I think there is some weird bubbling going on. because you only have two instances of your directive. This is what I see in the console log: before return compile compile controller pre post controller pre post
Abraham P at Stack Overflow Visit the source
Related Q & A:
- How to compile OpenCV on Windows?Best solution by Stack Overflow
- Where to put $watch in directive in AngularJS?Best solution by stackoverflow.com
- How to dynamically load directive in angularjs through json?Best solution by Stack Overflow
- How do I compile my Python program to a .jar with Jython?Best solution by Stack Overflow
- Is it possible to compile Swift for OS X 10.5?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.