AngularJS路由:URL中不必要的参数

angularJS routing: unnecessary params in url

本文关键字:不必要 参数 URL 路由 AngularJS      更新时间:2023-09-26

在我的应用程序中,如果我收到密码重置说明,我会使用如下网址转到服务器:

/changepass?key=1231231231212312

在控制器中,我有这样的代码:

  if (typeof $routeParams.key !== 'undefined') {
    $scope.changePassword();
  }

但是当我更改密码并想在同一视图中登录时,我去了另一个位置,但这个?key=123123123仍然存在。我做错了什么,如何清空:/公司没有任何钥匙?

$scope.login = function() {
        ***
                $location.path('/company');
                ***
      };

我也试过$scope.$apply($location.path('/company'));但是当我登录后去公司时,我的 url 中仍然有参数。如何解决这个问题?

在路由中:

    .when('/signin', {
        templateUrl: 'views/authorization.html',
        controller: 'AuthorizationCtrl'
    })
    .when('/changepass', {
        templateUrl: 'views/authorization.html',
        controller: 'AuthorizationCtrl'
    })
有两种

方法可以实现此目的

  • $location#url

$location.url($location.path('/company'));

  • $location.search

$location.search('key', null);

参见

  • $location文档

不相关:

当你使用angularjs时,你可以使用它的内置函数,如angular.isDefined

if(angular.isDefined($routeParams.key)){
     $scope.changePassword();
}