Angular 更改 URL 中变量的值

Angular change the value of a variable in URL

我目前遇到无法更改 URL 中参数值的问题。这是我的 URL : http://localhost/details

我想在过滤 table 时在 URL 中添加或交换一个 "inverse" 参数,如下所示: http://localhost/details?inverse=false or http://localhost/details?inverse=true 每次我提交是否选中反复选框的表单。

以下是如何从 URL 获取参数:

var paramsURL = $location.search();
// If params in URL
if( Object.keys(paramsURL).length > 0) {
    if ( paramsURL.hasOwnProperty('reverse')){
         $scope.reverse = paramsURL.reverse;
    }
}

如果 URl 有一个与 true 或 false 相反的参数,那么我会存储它并在请求中应用它。到目前为止效果很好。

问题是在用户提交表单时动态更改 URL。

我尝试了以下方法,但没有一个起作用:

$location.search('reverse', $scope.reverse).replace();

$location.search({reverse:$scope.reverse});

$scope.reverse 存在是因为它具有默认值,但我无法更改 URL 以匹配其值。

有没有人以前遇到过这个问题?谢谢您的帮助。

如果您的要求正确,您需要在查询参数中动态设置新值。您可以尝试关注

$location.path('/currentPath').search({key: value});

键: 将是 reverse

值:将动态设置为truefalse

以上 setter 方法允许链接多个值