Basically, what happens when a directive hits the image, angular first runs the compile directive function.
Here in compile , you can have control over the raw DOM / simple DOM, in compile you will have no scope. The compilation function is responsible for returning the preLink and postLink . From which preLink is called first
Inside the preLink function preLink you can have a DOM with a scope, immediately after preLink it displays an internal element or other directives. After passing through each DOM element, it invokes the postLink function. Which fully compiles the DOM on a grand scale. Therefore, the directive event is logged first because it has the lowest priority and has its own postLink function.
Plunkr to understand the flow (look at the console to make it more understandable)
You must register these events inside the preLink function preLink that the event is logged after compiling the directive function.
the code
angular.module('app', []) .directive('direc', function() { return { priority: 1, compile: function(element, attributes) { console.log("Compiled direc") return { pre: function(scope, element, attrs) { console.log("Prelink for direc firective generated"); element.on('click', function() { alert('direc'); }); }, post: function(scope, element, attrs) { console.log("Called After direc DOM linked with scope") } } }, } }) .directive('directive', function() { return { priority: 0, compile: function(element, attributes) { console.log("Compiled direc") return { pre: function(scope, element, attrs) { console.log("Prelink for direc firective generated"); element.on('click', function() { alert('directive'); }); }, post: function(scope, element, attrs) { console.log("Called After directive DOM linked with scope") } } }, }; });
Working plunkr