Avoid circular dependencies with AngularJS Directives

Avoid circular dependencies with #AngularJS Directives by @ythos

  • Post was not sent – check your email addresses!
  • If you enjoyed the post, please consider sharing it on social media using the buttons below.
  • The entry was posted in Development and tagged
  • Matthew was CTO of a venture-backed technology start-up in the UK & US for 10 years, and is now a Founder of Endjin Ltd, which provides technology strategy, experience and development services to its clients who are seeking to take advantage of Microsoft Azure and the Cloud.
  • App.factory(‘templateRepository’, [ ‘$injector’, function($injector) { function lookupUrlFor(contentType) { // Do your translation } return { getTemplate: function(contentType) { var templateCache = $injector.get(‘$templateCache’); return templateCache.get(lookupUrlFor(contentType)); } } }]);

endjin are a full-service design, experience, development & cloud consultancy, with deep experience of technology and a broad business expertise. endjin loves solving business problems by the smart application of technology and design

@endjin: Avoid circular dependencies with #AngularJS Directives by @ythos

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.

Matthew was CTO of a venture-backed technology start-up in the UK & US for 10 years, and is now a Founder of Endjin Ltd, which provides technology strategy, experience and development services to its clients who are seeking to take advantage of Microsoft Azure and the Cloud. You can follow Matthew on twitter.

Avoid circular dependencies with AngularJS Directives

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.