TypeScript+AngularJs:-How to define multiple routes in a single route file

774 views Asked by At

I have implemented a module using typescript and angular js where i have multiple pages i want to use one typescript controller per page.than how can i define them in my routes as right now i have defined one only but what do to if i have 6 to 7 controllers and pages too.I have shown my code below:-

/// <reference path="../scripts/typings/angularjs/angular.d.ts" />
/// <reference path="../scripts/typings/angularjs/angular-route.d.ts" />
module CustomerSearch {
    export class Routes {
        static $inject = ["$routeProvider"];
        static configureRoutes($routeProvider: ng.route.IRouteProvider) {
            $routeProvider.when("/search", {
                templateUrl: "search.aspx",
                controller: "CustomerSearch.controllers.CustomerCtrl"
            });
            $routeProvider.otherwise({ redirectTo: "/search" });
        }

        }
    }
2

There are 2 answers

0
Snm Maurya On BEST ANSWER

Use this syntax:

$routeProvider.
when("/search", {
  templateUrl: "search.aspx",
  controller: "CustomerSearch.controllers.CustomerCtrl"
}).
when('/home', {
  templateUrl: "home.aspx",
  controller: "ControllerName"
}).
when('/contact', {
  templateUrl: "contact.aspx",
  controller: "ControllerName"
}).
otherwise({ 
  redirectTo: "/search" 
});

0
Aditya Singh On

Just keep them chaining to the the $routeProvider.when

/// <reference path="../scripts/typings/tsd.d.ts" />

module CustomerSearch {
    export class Routes {
        static $inject = ["$routeProvider"];
        static configureRoutes($routeProvider: ng.route.IRouteProvider) {
            $routeProvider
    .when("/search", {
                 templateUrl: "search.aspx",
                 controller: "CustomerSearch.controllers.CustomerCtrl"
             })
    .when("/home", {
     templateUrl: "partials/home.aspx",
     controller: "home.controller"
    });
    
            $routeProvider.otherwise({ redirectTo: "/search" });
        }

        }
    }