【问题标题】:Angular $route change url based on routeAngular $route 根据路由更改 url
【发布时间】:2016-06-10 09:54:54
【问题描述】:

我正在使用 Angular js 编写一个简单的产品信息管理应用程序。为了使我的应用程序尽可能模块化,我将其拆分为多个模块,其中一个模块“pim”作为起点。对于每个模块,我希望有不同的路由,以便轻松插入新模块或删除它,而无需在 pim 模块配置中维护巨大的路由。

目前我有两条路线(第一条路线):

(function(){
angular 
    .module("pim")
    .config(router)

function router($routeProvider){
    $routeProvider
        .when("/",{
            templateUrl: "view/info.html",
            controller: "pimController"
        })
        .when("/info",{
            templateUrl: "view/info.html",
            controller: "pimController"
        })
        .when("/alcohol",{
            templateUrl: "view/alcohol.list.html",
            controller: "alcoholController"
        });
    }
})();

第二条路线

(function(){
angular 
    .module("alcohol")
    .config(router)

function router($routeProvider){
    $routeProvider
        .when("/alcohol/list",{
            templateUrl: "view/alcohol.list.html",
            controller: "alcoholController"
        })
        .when("/alcohol/info",{
            templateUrl: "view/alcohol.info.html",
            controller: "alcoholController"
        });
    }
})();

如您所见,/alcohol 有一个 templateUrl 和一个控制器,与 /alcohol/list 相同,但我想知道是否有一种简单(标准)的方法可以更改为另一个 URL,例如 /alcohol/list,这样我就不必重复 templateUrl 和控制器,并将这些信息保存在它所属的酒精模块中。

例如

.when("/alcohol",{
    routeTo: "/alcohol/list"
})

感谢您的帮助

已解决

存在重定向选项,在 $routeProvider 文档中看的不够好:

.when("/alcohol",{
     redirectTo:"/alcohol/list"
});

上面的代码有效

【问题讨论】:

    标签: javascript angularjs angular-ui-router


    【解决方案1】:

    您可以使用$routeProviderredirectTo 路线图。

    .when("/alcohol", {
      redirectTo: "/alcohol/list"
    });
    

    阅读更多:https://docs.angularjs.org/api/ngRoute/provider/$routeProvider

    【讨论】:

    • 我会说你解决了它,但我不确定你是否先阅读我的解决方案然后复制它。所以我会说它很有用
    • 不,我实际上浏览了文档并找到了它:) 我们同时找到它只是一个巧合。
    猜你喜欢
    • 2016-08-31
    • 2018-02-10
    • 2013-08-10
    • 2019-07-17
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多