Search code examples
angularjsngroute

routing sub-modules in angular


how to write different modules with their own routing?

i have an angular app and it has different modules.i am going to write for each of them, specific route file, but i got this error

Uncaught Error: [$injector:unpr] http://errors.angularjs.org/1.6.4/$injector/unpr?p0=routeServiceProvider%20%3C-%20routeService

it is my code :

sample.module.js

angular.module('app.sample', []);

sample.route.js

angular
.module('app.sample')
.run(appRun);

  /* @ngInject */
  function appRun (routeService) {
     routeService.configureRoutes(getRoutes());
  }

 function getRoutes () {
    return [ {
       url: '/sample',
       config: {
          templateUrl: 'sample.html'
       }
     }
    ];
}

i already add ngRoute and inject these files in index.html file


Solution

  • To achieve such project structure, ui-router is the best way to go. It is a separate library so you have to include into your project as a dependency.

    Here are the snippets that will be useful for your case

    dashboard.module.js

    angular.module('app.dashboard', ['ui.router']);
    

    dashboard.router.js

        angular.module('app.dashboard')
            .config(routerConfig);
    
        routerConfig.$inject = ['$stateProvider'];
        function routerConfig($stateProvider) {
            $stateProvider
                .state('state1', {
                    url: '/state1',
                    templateUrl: 'url/to/state1.html',
                    controller: function () {
                        // controller code here
                    }
                })
                .state('state2', {
                    url: '/state2',
                    templateUrl: 'url/to/state2.html',
                    controller: function () {
                        // controller code here
                    }
                });
        }
    

    sample.module.js

    angular.module('app.sample', ['ui.router']);
    

    sample.router.js

    angular.module('app.sample')
            .config(routerConfig);
    
        routerConfig.$inject = ['$stateProvider'];
        function routerConfig($stateProvider) {
            $stateProvider
                .state('state3', {
                    url: '/state3',
                    templateUrl: 'url/to/state3.html',
                    controller: function () {
                        // controller code here
                    }
                })
                .state('state4', {
                    url: '/state4',
                    templateUrl: 'url/to/state4.html',
                    controller: function () {
                        // controller code here
                    }
                });
        }
    

    Lastly, app.module that connects all these modules

    app.module.js

    angular.module('app', [
        /*
         * sub-modules
         */
        'app.dashboard',
        'app.sample'
    ]);
    

    To sum up, you have two independent sub-modules (app.dashboard and app.sample) with their own routing logic and one module (app) that wraps them into one angular application.

    $stateProvider, service provided by ui.router, is used for registering states.

    Additional info

    Since your application is modular, you are probably going to need nested routing which is greatly supported by ui.router. Read docs to get more information on nested states.

    Update

    However, if you still want to stick with ngRoute, this and this clearly explain how to achieve the same result.