Unable to access an angularjs module inside another module

53 views Asked by At

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?

0

There are 0 answers