Currently our project is using default $routeProvider
, and I am using this "hack", to change url
without reloading page:
services.service('$locationEx', ['$location', '$route', '$rootScope', function($location, $route, $rootScope) {
$location.skipReload = function () {
var lastRoute = $route.current;
var un = $rootScope.$on('$locationChangeSuccess', function () {
$route.current = lastRoute;
un();
});
return $location;
};
return $location;
}]);
and in controller
$locationEx.skipReload().path("/category/" + $scope.model.id).replace();
I am thinking of replacing routeProvider
with ui-router
for nesting routes, but cant find this in ui-router
.
Is it possible - do the same with angular-ui-router
?
Why do I need this?
Let me explain with an example :
Route for creating new category is /category/new
after clicking
on SAVE I show success-alert
and I want to change route /category/new
to /caterogy/23
(23 - is id of new item stored in db)
Simply you can use
$state.transitionTo
instead of$state.go
.$state.go
calls$state.transitionTo
internally but automatically sets options to{ location: true, inherit: true, relative: $state.$current, notify: true }
. You can call$state.transitionTo
and setnotify: false
. For example:can be replaced by
Edit: As suggested by fracz it can simply be:
I don't think you need ui-router at all for this. The documentation available for the $location service says in the first paragraph, "...changes to $location are reflected into the browser address bar." It continues on later to say, "What does it not do? It does not cause a full page reload when the browser URL is changed."
So, with that in mind, why not simply change the $location.path (as the method is both a getter and setter) with something like the following:
The documentation notes that the path should always begin with a forward slash, but this will add it if it's missing.
Ok, solved :) Angular UI Router has this new method, $urlRouterProvider.deferIntercept() https://github.com/angular-ui/ui-router/issues/64
basically it comes down to this:
I think this method is currently only included in the master version of angular ui router, the one with optional parameters (which are nice too, btw). It needs to be cloned and built from source with
The docs are accessible from the source as well, through
(they get built into the /site directory) // more info in README.MD
There seems to be another way to do this, by dynamic parameters (which I haven't used). Many credits to nateabele.
As a sidenote, here are optional parameters in Angular UI Router's $stateProvider, which I used in combination with the above:
what that does is it allows to resolve a state, even if one of the params is missing. SEO is one purpose, readability another.
In the example above, I wanted doorsSingle to be a required parameter. It is not clear how to define those. It works ok with multiple optional parameters though, so not really a problem. The discussion is here https://github.com/angular-ui/ui-router/pull/1032#issuecomment-49196090
After spending a lot of time with this issue, Here is what I got working
If you need only change url but prevent change state:
Change location with (add .replace if you want to replace in history):
Prevent redirect to your state:
Try something like this