0

Im building a big app , and my structure goes like this:

Each module has its own folder structure for controllers, directives, etc...

Each folder has an index.js file and then other files to separates each controller, each directive, etc...

The index.js file contains the definition of the module. For instance for the controllers of the businessModule above:

angular.module('myCompany.businessModule.controllers', []);

There's no dependencies here, but there could be any.

Then in firstCtrl.js, I can reuse that module and add the controller to it:

angular.module('myCompany.businessModule.controllers').controller('firstCtrl', function(){
});

Then the app.js aggregates all the module that I want for my application by adding them to the dependencies array.

 angular.module('myApp', ['myCompany.businessModule', 'myCompany.anotherBusinessModule'],'ngRoute');

Now comes the route that include the other modules view. My Route goes like:

    var myApp = angular.module('myApp');
    myApp.config(function($routeProvider) {
        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'firstCtrl'
            })

    });

Now the question is can i connect a controller that from another module to the specific controller that have this module injected like in my situation when myApp contains myCompany.businessModule.controllers and has firstCtrl ?

1
  • Have you tried running this, what error are you getting Commented Jan 27, 2016 at 9:39

1 Answer 1

1

Your modules file :

   angular.module('myApp').config(function($routeProvider){
           $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController'
            })

            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'firstCtrl'
            });
});

Include it as a dependency when you want to use it.

angular.module('app',[other deps]);
Sign up to request clarification or add additional context in comments.

1 Comment

Well if the module included i can use any controller in the other module ? And what about two controllers with the same name ?

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.