简体   繁体   English

替换浏览器历史记录的Angular $ location.search

[英]Angular $location.search with replacing browser history

There is the following situation. 存在以下情况。 I've at '/drafts/ URL, and I go to 'drafts/101' (101 is "id" params). 我使用的是'/ drafts / URL,然后转到'drafts / 101'(101是“ id”参数)。 In this controller I want to change 101 to 102 without page refreshing; 在此控制器中,我想将101更改为102而无需刷新页面; I don't know how I can do it right, so I do the following thing: 我不知道该怎么做,所以我做以下事情:

$location.search('new_id', message.id).replace()

It's good, but when I press 'back' button I go to '/drafts?new_id=102' URL, but I need '/drafts' only. 很好,但是当我按“后退”按钮时,我转到“ / drafts?new_id = 102” URL,但是我只需要“ / drafts”。 How can I do it? 我该怎么做? You can suggest some solution to my trouble or how I can change 'id' entirely, without '?new_id'. 您可以提出一些解决方案,也可以提出一些解决方案,或者如何在没有'?new_id'的情况下完全更改'id'。 Thanks in advance. 提前致谢。

This should help. 这应该有所帮助。

Thanks had just provided query string param. 感谢刚刚提供的查询字符串参数。 Update added below for route param. 下面为路由参数添加了更新。 No direct way for route params other than to provide a back button. 除了提供后退按钮外,没有其他直接用于路线参数的方法。 http://stackoverflow.com/questions/15175429/angularjs-getting-previous-route-path

<!DOCTYPE html>
<html ng-app="demo">
<head>
    <meta charset="UTF-8">
    <title>Ang</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
    <script src="angular-route.js"></script>
    <script>
        var app = angular.module("demo",['ngRoute']);
        app.config(function($routeProvider){
            $routeProvider
            .when("/",{
                template:"<div>{{id}}</div>",
                controller:function($scope){
                    $scope.id = "Home";
                }
            })
            .when("/demo",{
                template:"<div>{{id}}</div>",
                controller:function($scope){
                    $scope.id = "Demo";
                }
            })  
            .when("/demo/:name",{
                template:"<a href='' ng-click='back();'>Back</a><div>{{id}}</div>",
                controller:function($scope, $route,$routeParams,$location,$rootScope){
                    // Do the following for routeParams
                    // But now the back will not work. 
                    // So you might have to use a back button or provide link to url:/demo      
                    if($routeParams.name === 'ganesh'){$route.updateParams({name:'test'});}
                    $scope.back = function(){
                        $rootScope.back();
                    }
                    // Do the following for Query Params  (/demo?name=...) and ('/demo?name')
                    //console.log($routeParams.name);
                    //$scope.id = $location.search().name;
                    //if ($location.search().name === 'test'){$location.search('name', 'ganesh').replace()};
                }
            })  
            .otherwise("/");        
        }).run(function ($rootScope, $location) {
                var history = [];
                $rootScope.$on('$locationChangeStart', function() {
                    history.push($location.$$path);
                });
                $rootScope.back = function () {
                    var prevUrl = history.length > 2 ? history.splice(-3)[0] : "/";
                    console.log(prevUrl);
                    $location.path(prevUrl);
                };
        });
    </script>
</head>
<body>
    <div ng-view></div>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM