javascript - AngularJS路由: unnecessary params in url

标签 javascript angularjs url-routing

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

/changepass?key=1231231231212312

在 Controller 中我有这样的代码:

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

但是当我更改密码并想要在同一 View 中登录时,我转到另一个位置,但这个 ?key=123123123 仍然在那里。我做错了什么,如何在没有任何 key 的情况下进入空:/company?

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

我也尝试了$scope.$apply($location.path('/company')); 但当我登录后去公司时,我的网址中仍然有参数。怎么解决这个问题?

在路由中:

    .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);

另请参阅

<小时/>

不相关:

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

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

关于javascript - AngularJS路由: unnecessary params in url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28167736/

相关文章:

javascript - 为什么我的 javascript 运行时间很长?

php mvc 和 .htaccess url 重写

javascript - AngularJS 路线

javascript - 在 Mongoose 中加载后更改模型值

javascript - 在 Vue.js 中触发自定义 "change"事件是否安全?

javascript - 元素上的多次单击绑定(bind)仅执行 Knockout.js 中的一个?

javascript - 使用 groupBy 进行多次 ng-repeat

javascript - 使用 angularjs 将复选框设置为选中时导致错误

html - 在 Angular 中显示包含 HTML 标记的字符串

angular - 在 Angular8 的动态 View 中添加友好的 Url 以使用 SEO