0

There is the following situation. I've at '/drafts/ URL, and I go to 'drafts/101' (101 is "id" params). In this controller I want to change 101 to 102 without page refreshing; 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. How can I do it? You can suggest some solution to my trouble or how I can change 'id' entirely, without '?new_id'. Thanks in advance.

malcoauri
  • 11,904
  • 28
  • 82
  • 137
  • What you are using for $location.search().replace() works with query names replacement. You will have to use $route.updateParams for routes. However, $route.updateParams surely will not work with back button of browser since you will have constant redirects. You will have to give a back link. You should try the above with query rather than routes, there are way you can achieve that and is easier to handle and is a known issue. If the query route is not working do put your code here for query params related issue. – Gary Dec 08 '15 at 18:01

1 Answers1

0

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>
Gary
  • 2,293
  • 2
  • 25
  • 47
  • 2
    This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether. – EnigmaRM Dec 08 '15 at 15:09
  • Made the change. Missed asking the question in the author comment section. Removed it now. – Gary Dec 08 '15 at 17:56