我有一个点击功能。在第一次点击功能中,我像这样为现有网址添加键值对

$scope.OnItemClick = function (bedroom) {
    $location.path("/builders/"+r.id).search({bedrooms:bedroom});
}


所以在其余功能中。我想根据现有网址添加额外的键值对

$scope.range = function (range) {
    $location.path("/builders/"+r.id).search({budget:range});
}

$scope.status = function (status) {
    $location.path("/builders/"+r.id).search({status:status});
}


我试图添加键值对,但这样使彼此过度。

1st click : ../#/builders/1?bedrooms=1
2nd click :../#/builders/1?budget:5(overriding)
3rd click :../#/builders/1?status:9(overriding)


所以我想基于单击功能这样做:

../#/builders/1?budget=5&bedrooms=1&status=9
../#/builders/1?bedrooms=1&status=9&budget=5
../#/builders/1?bedrooms=1&budget=5&status=9

最佳答案

$ location返回当前URL的搜索部分。

您可以执行以下操作:

$scope.range = function (range) {
    var current = $location.search();
    current.budget = range;
    $location.search(current);
}

关于javascript - 如何将键值对附加到AngularJS中已存在的键值对的url?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39823978/

10-08 23:30