Avoid circular dependencies with AngularJS Directives
![Avoid circular dependencies with AngularJS Directives](https://res.cloudinary.com/endjin/image/upload/f_auto/q_80/assets/images/blog/2014/11/Avoid-Circular-Dependencies-AngularJS-Directives-P1-1024px.png)
A problem you may encounter if you try to implement my previous example in your own code, is that your implementation of templateRepository cannot (apparently) make use of $templateCache, because angular will throw a circular dependency error.
To avoid this, you can inject the $injector service into the templateRepository, and defer the dependency resolution until you make the call.
Something like this:
app.factory('templateRepository', [ '$injector', function($injector) {
function lookupUrlFor(contentType) {
// Do your translation here
}
return {
getTemplate: function(contentType) {
var templateCache = $injector.get('$templateCache');
return templateCache.get(lookupUrlFor(contentType));
}
}
}]);