angularjs nested controller does not receive injected service

user3561344

I clearly do not understand the scope issues that I sense must be at play here. Despite having read quite a bit about scope in angularjs, and seeing it work as expected in many cases.

But here I have 2 nested controllers:

MainController [can inject RepositoryService]
 --> ChallengeController [cannot successfully inject RepositoryService]
    --> SingleChallengeController [cannot successfully inject RepositoryService]
 --> PlayerController [can inject RepositoryService]

MainController is instantiated via the $routeProvider setup in app.js:

myApp.config(function ($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: 'views/main.html',
      controller: 'MainController'
    })
    .otherwise({
      redirectTo: '/'
    });
});

And here is the app declaration:

...
  <body ng-app="myApp">
    <div id="m" class="section app-viewport" ng-view=""  ng-keydown="keyPress($event);"></div>
...

Meanwhile the other 3 controllers are specified inline in main.html like so:

main.html:

    <div id="viewport" viewport ng-hide="isPhone && isLandscape" class="section main">
      <div class="container-fluid">
        <div main-panel-row-maximize-height class="row main-panel-row">
          <div class="main-panel">
            <div class="row">
              <div id="challenge-options-results" challenge-panel-init class="challenge-option-results display-visible" >
                <section ng-controller="ChallengeController" class="challenge-container">
                  <div class="container-fluid">
                    <div class="row">
                  <div ng-controller="SingleChallengeController"/>
                </div>
              </div>
            </section>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="display-hidden" id="controls-and-badges">
    <div id="readout" ng-controller='PlayerController'  class="balance">
    <span class="readout-label balance">data</span><hr class="balance-separator">
      <span class="amount" ng-bind="currData">$NaN</span>
    </div>
...

I am attempting to inject the service ('RepositoryService') into these controllers using the 'constructor' like so:

var challengeControllers = angular.module('challengeControllers', []);

challengeControllers.controller('ChallengeController', ['$scope',  function($scope,  eventBusService, RepositoryService) {
...

This way of injecting works fine for MainController and PlayerController, but not the other two.

Here is the constructor for the service I want to inject:

var serviceModule = angular.module('RepositoryService', []);

serviceModule.factory("RepositoryService", ['$rootScope', function ($rootScope, $http) {

Is there anything obvious I am missing that suppresses what appears to me to be logical behavior associated with dependency injection?

Thanks very much for your help.

artch

You should write your controller definition like this:

challengeControllers.controller('ChallengeController', ['$scope', 'eventBusService', 'RepositoryService', function($scope,  eventBusService, RepositoryService) {

It seems like you forgot to add eventBusService and RepositoryService in the array arguments here.

By the way, if you don't have the need to get this code obfuscated in production, just don't mess with explicitly named injectables and write like this:

challengeControllers.controller('ChallengeController', function($scope,  eventBusService, RepositoryService) {

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does AngularJS 1.4.8 app not work when service is injected into second controller?

From Dev

injected service is not being identified in the controller

From Dev

Service undefined when injected into controller

From Dev

AngularJS Service Undefined When Injected

From Dev

How do I update a value in an AngularJS (with TypeScript) controller when a value on an injected service updates

From Dev

AngularJS ControllerAs syntax and controller injected variables

From Dev

AngularJS: Factory is always undefined when injected in controller

From Dev

AngularJS: var in resolve is not injected in controller (unknown provider)

From Dev

AngularJS: Factory is always undefined when injected in controller

From Dev

Angularjs Uncaught ReferenceError on injected service into directive

From Dev

Does $scope is automatically injected to controller in Angular js?

From Dev

AngularJS Controller not working in nested Controller

From Dev

Controller in Nested views of AngularJS

From Dev

Controller in Nested views of AngularJS

From Dev

AngularJS controller and service creation

From Dev

Injecting Angularjs service into controller

From Dev

AngularJS: controller vs service

From Dev

AngularJs, inject service in controller

From Dev

AngularJS Service -> Controller population

From Dev

AngularJs, inject service in controller

From Dev

Error: error:unpr Unknown Provider injected service in controller

From Dev

Grails injected Mail Service bean is null inside a controller

From Dev

How do I test a controller that watches for changes on an injected service?

From Dev

Error: error:unpr Unknown Provider injected service in controller

From Dev

AngularJS: Mock object injected to controller trough router's resolve

From Dev

AngularJS: Mock object injected to controller trough router's resolve

From Dev

AngularJs multiple instances and nested controller

From Dev

AngularJS service injected in two different app modules, two apps

From Dev

AngularJS service update controller variable

Related Related

  1. 1

    Why does AngularJS 1.4.8 app not work when service is injected into second controller?

  2. 2

    injected service is not being identified in the controller

  3. 3

    Service undefined when injected into controller

  4. 4

    AngularJS Service Undefined When Injected

  5. 5

    How do I update a value in an AngularJS (with TypeScript) controller when a value on an injected service updates

  6. 6

    AngularJS ControllerAs syntax and controller injected variables

  7. 7

    AngularJS: Factory is always undefined when injected in controller

  8. 8

    AngularJS: var in resolve is not injected in controller (unknown provider)

  9. 9

    AngularJS: Factory is always undefined when injected in controller

  10. 10

    Angularjs Uncaught ReferenceError on injected service into directive

  11. 11

    Does $scope is automatically injected to controller in Angular js?

  12. 12

    AngularJS Controller not working in nested Controller

  13. 13

    Controller in Nested views of AngularJS

  14. 14

    Controller in Nested views of AngularJS

  15. 15

    AngularJS controller and service creation

  16. 16

    Injecting Angularjs service into controller

  17. 17

    AngularJS: controller vs service

  18. 18

    AngularJs, inject service in controller

  19. 19

    AngularJS Service -> Controller population

  20. 20

    AngularJs, inject service in controller

  21. 21

    Error: error:unpr Unknown Provider injected service in controller

  22. 22

    Grails injected Mail Service bean is null inside a controller

  23. 23

    How do I test a controller that watches for changes on an injected service?

  24. 24

    Error: error:unpr Unknown Provider injected service in controller

  25. 25

    AngularJS: Mock object injected to controller trough router's resolve

  26. 26

    AngularJS: Mock object injected to controller trough router's resolve

  27. 27

    AngularJs multiple instances and nested controller

  28. 28

    AngularJS service injected in two different app modules, two apps

  29. 29

    AngularJS service update controller variable

HotTag

Archive