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 />
</third-party-dir>
Whenever an Item is added i want to execute some code depending on third-party-dir
. However my-additional-dir
is instanciated first and thus the third-party-dir
-link
function wasn't executed yet. I tried to add dependencies by using "require" but since thirdPartyDir
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 third-party-dir
to be instanciated before executing the code within my directive?
When declaring your directive you can use priority
to decide the order in which the link functions are called.
https://docs.angularjs.org/api/ng/service/$compile#-priority-
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.
.directive('myAdditionalDir', function(){
return {
priority: -1