How to split AngularJS application into smaller modules and handle routing correctly?

后端 未结 4 1677
半阙折子戏
半阙折子戏 2020-12-24 07:18

What would be the best way to split AngularJS application into smaller pieces/module? For example if I have a blog post and commenting enabled for that, I t

4条回答
  •  隐瞒了意图╮
    2020-12-24 07:27

    I would divide the app in "view modules" and these sub modules.

    Then I use the $routeProvider to switch between the views. I define the different routing config in each module.

    If I need further submodules, I load these with ng-include.

    /* App Module */
    
    angular.module('MyApp', ['MyApp.home', 'MyApp.blog'])
    .config( function myAppConfig ( $routeProvider ) {
        'use strict';
        $routeProvider.otherwise({ redirectTo: '/home' });
    });
    
    
    /* home Module */
    
    angular.module('MyApp.home', [])
    .config(['$routeProvider', function config( $routeProvider ) {
      $routeProvider.when('/home', {
        controller: 'HomeController',
        template: '

    This is my Home

    ' }); }]);

    I created a little repository on github to explain this.

提交回复
热议问题