0

I have an angularJS app with three modules: myApp(parent/core module), services(contains all the factories and services), and controllers(contains all the controllers).

This is my app.js file

angular.module('services', []);
angular.module('controllers', []);
var app = angular.module('myApp', ['services', 'controllers']);

I read here that setting up my modules and defining dependencies in this way will allow each of my modules to access the other without having to inject the dependencies.

Here's my index.html file

<script src="/js/app.js"></script>
<script src="/js/services/testFactory.js"></script>
<script src="/js/controllers/testCtrl.js"></script>

testFactory.js file

angular.module('services').factory('testFactory', ['$rootScope', 'apiCall', function($rootScope, apiCall){
    let testFactory = {};

    testFactory.testFunc = function(){
        console.log('testFactory.testFunc called');
    }

    return testFactory;
}])

testCtrl.js file

angular.module('controllers').controller('testCtrl', ['$scope', 'testFactory', function($scope, testFactory){
    console.log("inside testCtrl");

    $scope.testing = function(){
        console.log("testing function called");
        testFactory.testFunc();
    }
}])

When I call the testFactory.testFunc inside the myApp module, it functions correctly. But, when I try calling it in the controller module, I'm getting the following error

TypeError: Cannot read property 'testFunc' of undefined

I even tried injecting the service module inside the controller module as a dependency, but I still got the same error.

How do I get the service module to work inside the controller module?

2
  • Check for a array-annotation injection missmatch. The names in the injection array should match the arguments in the injected function. Commented Oct 4, 2019 at 20:25
  • @georgeawg that solved it. Thanks a ton! Commented Oct 5, 2019 at 7:47

0

Your Answer

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