Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update the url in angular js with $route and $routeParams?

How can I redirect or update the url? I cannot find any good documentation regarding this. Basically, what I want to do is to change the $routeParams dynamically and update the url with the new value.

My code looks like this:

if ($routeParams.time) {
        var url;
        $routeParams.time = encodeURIComponent(value);
        url = '/' + $routeParams.time + '/' + 'marketing/networks';
        $location.path(url);
    } else {
        $routeParams.time = encodeURIComponent(value);
        url =  '/' + $routeParams.time + $location.path();
        $location.path(url);
    }
like image 761
Mythriel Avatar asked Oct 09 '12 09:10

Mythriel


2 Answers

After reading the comments to my answer, I think maybe is not the right answer for this case. Please, before using this solution, read the comments and other answers. I'm not using Angular anymore so I don't feel qualified for answering.

I leave the original answer unmodified below:

You are changing the location properly but AngularJS it's not realizing that it changed. You can solve the problem using the method '$apply' of your scope like this:

$location.path( url );
$scope.$apply();

Or like this:

$scope.$apply( $location.path( url ) );

See $apply documentation here http://docs.angularjs.org/api/ng.$rootScope.Scope

like image 175
Robert Avatar answered Oct 01 '22 21:10

Robert


You might want to try native browser object $window.location.href instead, according to http://docs.angularjs.org/guide/dev_guide.services.$location (in Caveats section).

like image 44
Tosh Avatar answered Oct 01 '22 20:10

Tosh