Angularjs $location.path(…)将ID添加到url

Angularjs $location.path(…)将ID添加到url,angularjs,Angularjs,如果用户已注销,我想将用户发送到登录页面,但失败。我有以下配置: webShopApp.config(function($routeProvider) { $routeProvider.when('/login', { templateUrl: 'partials/login.html', controller: 'LoginController' }); $routeProvider.when('/:id?/?products',

如果用户已注销,我想将用户发送到登录页面,但失败。我有以下配置:

webShopApp.config(function($routeProvider) {
    $routeProvider.when('/login', { 
        templateUrl: 'partials/login.html', 
        controller: 'LoginController' 
    });
    $routeProvider.when('/:id?/?products', {
        templateUrl: 'partials/products.html', 
        controller: 'MenuController' 
    });
});
请注意产品代码中的“/:id?/?”。假设url是“index.html#/45/products”。如果控制器发现用户已注销,它将调用

$location.path('/login');

如果我在$location.path()中输入console.log,它会按预期显示“/login”,但浏览器中的url是“index.html#/45/login”。为什么“45”(id)仍然存在,我如何摆脱它,即重定向到“index.html#/login”,而不使用前面路径中的id?

我想你应该使用
window.location='yourUrl'
而不是
$location.path('/login')
;在这种情况下。但是在这种情况下,页面会被重新加载。

请查看是否需要使用纯angularjs进行训练,因此请避免使用javascript术语和jquery术语,即windows.location和$location.path;更好的选择是,每当用户从应用程序注销时,总是将用户路由到登录页面。 为此,您可以在路由级别指定它,即

$routeProvider.otherwise({redirectTo: "/login"});
或者,您可以在单击哪个用户将被重定向到登录页面时说一个注销链接

$routeProvider.when('/logout', { 
        templateUrl: 'partials/login.html', 
        controller: 'LoginController' 
    });

我希望这可能会有所帮助。

根据本文档,$location.path是正确的选择:。请看该文档仅说明windows.location和$location.path之间的区别,或者它更多地说明了哪一个与angularjs兼容,所以您尝试过我的建议吗?之后,它会说:“何时应使用$location?当您的应用程序需要对当前URL的更改做出反应时,或者如果您想在浏览器中更改当前URL时,“这正是我想要做的。在ajax调用中,后端会以401响应,在我们的示例中,这意味着用户已注销(会话已超时)。然后我需要重定向到登录表单。我不明白如何使用$routeProvider。何时(…)