How might I move AngularJS Routes into separate file

后端 未结 3 1839
滥情空心
滥情空心 2021-02-01 07:51

I was curious if anybody was familiar with separating routes from the main app config function. My route list is getting quite large and I wanted to move them into a separate fi

3条回答
  •  悲&欢浪女
    2021-02-01 08:37

    It's easy to set up config file separately. There are few other ways to set this up, and I played around with those structure for config; this seems to work for me the best. Enjoy!

    ---> myApp.html

    
        
               
               
               
               
               ...
            
            
    
                 
                

    ----> app.js

    'use strict';
    
    angular.module('myApp', [
        'ngRoute',
        'ngAnimate',
        'myApp.controllers'
    ]).
    config(['$routeProvider', function ($routeProvider) {     
        $routeProvider.when('/page1', {
            templateUrl : 'partials/page1.html',
            controller : 'page1Controller'  
        });
    
        $routeProvider.when('/page2', {
            templateUrl : 'partials/page2.html',
            controller : 'page2Controller'  
        });
    
        $routeProvider.when('/images', {
            templateUrl : 'partials/page3.html',
            controller : 'page3Controller'
        });
    
        $routeProvider.otherwise({redirectTo: '/page1'});
    }]);
    

    --->controller.js

    angular.module('myApp.controllers', ['myModules'])
    
    .controller('mainCtrl', function($scope) {
      ...
    })
    
    .controller('page1', function($scope) {
      ...
    })
    
    .controller('page2', function($scope) {
      ...
    })
    
    .controller('page3', function($scope) {
      ...
    });
    

提交回复
热议问题