【问题标题】:TypeScript+AngularJs:-How to define multiple routes in a single route fileTypeScript+AngularJs:-如何在单个路由文件中定义多条路由
【发布时间】:2015-10-15 04:13:45
【问题描述】:

我已经使用 typescript 和 angular js 实现了一个模块,其中我有多个页面,我想每页使用一个 typescript 控制器。而不是我如何在我的路由中定义它们,因为现在我只定义了一个,但是如果我也有 6 到 7 个控制器和页面。我在下面显示了我的代码:-

/// <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" });
        }

        }
    }

【问题讨论】:

    标签: angularjs typescript angular-routing typescript1.4 typescript1.5


    【解决方案1】:

    使用以下语法:

    $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" 
    });

    【讨论】:

      【解决方案2】:

      只要让他们链接到$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" });
              }
      
              }
          }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2017-11-02
        • 2020-02-19
        • 1970-01-01
        • 2021-02-24
        相关资源
        最近更新 更多