0

I'm loading a module on my base html page called userFromServer. I'd like to inject it into both the main app module and also the mixpanel module. However I'm getting an injection error if I try to inject userFromServer into analytics.mixpanel(angular mixpanel). Is this a circular dependency error or am I missing something? should userFromServer be available to all modules? How can I inject userFromServer into both modules?

var app  = angular.module('App', ['userFromServer', 'analytics.mixpanel']) 
// main app with currentUser injectable
var mixp = angular.module('analytics.mixpanel', ['userFromServer'])
mixp.config(['$mixpanelProvider', 'currentUser', function($mixpanelProvider, currentUser) {
 // use currentUser here
}]);

userFromServer module

<body ng-app="App" ng-cloak>
  <base href="/">

  <div class="container">
    <div ng-view=""></div>
  </div>

  <script type="text/javascript">
    angular.module('userFromServer', [])
      .service('currentUser', function() {
        <% if logged_in %>
          this.name = '<%= @User.name %>';
          this.first_name = '<%= @User.first_name %>';
          this.id = '<%= @User.id %>';
          this.uuid = '<%= @User.profile.uuid %>';
        <% end %>
      })
  </script>


</body>

the stack trace is:

Failed to instantiate module App due to:

Failed to instantiate module analytics.mixpanel due to:

Unknown provider: currentUser
3
  • what is the error that you are getting? also please include the userFromServer modelu definition. Commented Sep 7, 2016 at 13:19
  • Please, provide full error message (including call stack) and the code for relevant modules. The listed code doesn't contain anything that could cause an error (except the fact that there's no userFromServer). Also, you're confusing injection with loading. Modules are being loaded, there's no problem with circular dependency. Dependencies are injected. Commented Sep 7, 2016 at 13:19
  • @estus updated with code and stack trace Commented Sep 7, 2016 at 13:40

2 Answers 2

1

This happens because currentUser service instance is injected into config block. Only service providers can be injected here.

Since currentUser is a constant object that doesn't depend on other services and it should be available during config phase, it may be constant service:

angular.module('userFromServer', [])
  .constant('currentUser', {
    <% if logged_in %>
      name: '<%= @User.name %>',
      first_name: '<%= @User.first_name %>',
      id: '<%= @User.id %>',
      uuid: '<%= @User.profile.uuid %>'
    <% end %>
  })
Sign up to request clarification or add additional context in comments.

Comments

1

Since your parent module App already has userFromServer injected in it, you don't need to inject it again in analytics.mixpanel.

3 Comments

Actually, you need to load it for consistency it if both modules depend on this module. This is what modularity is about. Modules can be used or tested apart from each other this way.
@estus The parent module App has already listed both analytics.mixpanel and userFromServer as it's dependency. So there's no need to inject userFromServer as a dependency separately for sub-module analytics.mixpanel again. I agree that if two completely independent modules have a dependency, they you can inject it separately, but here they are not independent, rather 1st module App already has 2nd module analytics.mixpanel as it's dependency.
If App directly depends on userFromServer and analytics.mixpanel is removed from the app at some point, the app will stop working. Angular allows modules to be loaded in several places exactly to cover such cases, this is a feature of the framework and there's no need to avoid this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.