angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Option on $location to allow hash/path change w/o reloading the route

Open cgross opened this issue 13 years ago • 194 comments

We have a paradigm in our app that user's create new things on the same page that they view things. So our route is like /thing/:id. When creating a new thing they go to /thing/new. Once the thing has been successfully saved we want to change the route to /thing/1234 (or whatever its new id is). Now the partial doesnt need to be reloaded because the data is all the same. We just want the path to be updated so a user can now bookmark the correct link, etc.

Having an option on $location (not on the route definition) to enable/disable route loading would work but I'm sure there are other ways to implement the feature.

cgross avatar Dec 12 '12 18:12 cgross

+1, having the same issue.

qualidafial avatar Dec 17 '12 07:12 qualidafial

@cgross Since the $location service uses JQuery-style method chaining, and all commands are deferred until $digest anyway, I suggest adding a new chaining method, e.g. reload(boolean):

$location.path("/thing/"+thing.id).replace().reload(false)

P.S. Is this the Chris Gross that started the Eclipse Nebula project?

qualidafial avatar Dec 17 '12 07:12 qualidafial

Yup :)

cgross avatar Dec 17 '12 14:12 cgross

+1 :+1:

celmaun avatar Dec 18 '12 17:12 celmaun

+1

Right now, we're using a dirty hack to get around this problem.

    $scope.$on('$locationChangeSuccess', function(event) {

        // Want to prevent re-loading when going from /dataEntry/1 to some other dataEntry path
        if ($route && $route.current && $route.current.$route.templateUrl.indexOf('dataEntry') > 0) {
            $route.current = lastRoute; //Does the actual prevention of routing
        }
});

shahmirn avatar Dec 18 '12 17:12 shahmirn

+1

stryderjzw avatar Jan 28 '13 03:01 stryderjzw

+1

chicoxyzzy avatar Feb 04 '13 12:02 chicoxyzzy

+1

evrijkom avatar Feb 04 '13 13:02 evrijkom

+1

tnajdek avatar Mar 18 '13 15:03 tnajdek

+1

sylvain-hamel avatar Apr 12 '13 00:04 sylvain-hamel

Here is my attempt at fixing this by supporting preventDefault() for $routeChangeStart. What do you think about that?
Commit : https://github.com/redhotsly/angular.js/commit/f8ac46e6ac9d76cf855077d21b68b4c2c8043db1

sylvain-hamel avatar Apr 13 '13 06:04 sylvain-hamel

I've been working into @qualidafial approach for a few days and now the PR is ready. I've created a new notify method that allows skipping the location change notification for one time. This should avoid triggering any route change or reload.

This doesn't touch the routing system, so you don't have to change your route definitions. It will still work even if you use ui-router instead of the standard AngularJS routing system.

Example: $location.path('/client/2').replace().notify(false);

I've also worked some tests and documentation bits.

lrlopez avatar Apr 13 '13 17:04 lrlopez

+1

cburgdorf avatar Apr 13 '13 19:04 cburgdorf

another +1 Using @lrlopez' pull request already. Works like a charm.

tiff avatar Apr 17 '13 10:04 tiff

+1

cayblood avatar Apr 17 '13 11:04 cayblood

+1

mlegenhausen avatar Apr 27 '13 16:04 mlegenhausen

Unfortunately PR https://github.com/angular/angular.js/pull/2398 has been rejected by the Angular team as skipping notifications could lead to inconsistencies between the actual URL and the current route. There is a long explanation at the end of the discussion.

I don't need this feature in any of my projects, but I'll keep it in my repository so it can be merged if you wish. Just let me know if it gets out of sync with the master branch so I can rebase the changes. Thanks!

lrlopez avatar Apr 28 '13 00:04 lrlopez

+1

camshaft avatar May 25 '13 01:05 camshaft

+1, need this :(

keabard avatar Jun 04 '13 09:06 keabard

+1

olanod avatar Jun 19 '13 19:06 olanod