sibaspage sibaspage - 3 months ago 7
AngularJS Question

how to write bunch of pages efficently using angular js ng-route

If I want to add more pages approximately to 100 so how can I write the below code? I just want the shortcut of "when" which is given below. I don't want to write the "when" 100 times.

var module = angular.module("sampleApp", ['ngRoute']);

module.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/route1', {
templateUrl: 'angular-route-template-1.jsp',
controller: 'RouteController'
}).
when('/route2', {
templateUrl: 'angular-route-template-2.jsp',
controller: 'RouteController'
}).
otherwise({
redirectTo: '/'
});
}]);

module.controller("RouteController", function($scope) {

})

Answer
module.config(['$routeProvider', 'routes'

  function($routeProvider, routes) {
    // routes is the list of my routes
    // e.g. 
    //  routes = [{
    //    url: '/about',
    //    options: {
    //      templateUrl: '/about.html',
    //      controller: 'AboutController as vm',
    //    }
    //  }]

    routes.forEach(function(route) {
      $routeProvider.when(route.url, route.options);
    });

    $routeProvider.
    otherwise({
      redirectTo: '/'
    });
  }
]);
Comments