H W H W - 1 year ago 62
AngularJS Question

Dependencies of directives

I am using a third party-directive in my angular app. I need to extend the functionality with my own directive and ran into some troubles with the order in which the directives are instanciated.

The directives are intended to be used like this:

<third-party-dir ng-repeat="item in items" my-additional-dir>
<some-content />

Whenever an Item is added i want to execute some code depending on
. However
is instanciated first and thus the
function wasn't executed yet. I tried to add dependencies by using "require" but since
does not use a controller (only the link function), angular is throwing an error.

.directive('myAdditionalDir', function(){
return {
require: '^thirdPartyDir'
restrict: 'A'
link: function(scope, element, attrs){
if (scope.$last){
//some code


Error: [$compile: ctreq] Controller 'thirdPartyDir', required by directive 'myAdditionalDir', can't be found!

I looked into pre- and post-link functions, but the default link-function is already a post-link function. Any hints on how i can "wait" for the
to be instanciated before executing the code within my directive?

Answer Source

When declaring your directive you can use priority to decide the order in which the link functions are called.


The post-links are called in order from lowest to highest. You could look at the third-party source code to spot its order and set yours lower.
The default is zero, but I'm not sure what happens if you set yours to -1.

.directive('myAdditionalDir', function(){
    return {
        priority: -1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download