change $location.path() but prevent route change?

32 views
Skip to first unread message

Nikita Tovstoles

unread,
Jun 3, 2014, 6:57:33 PM6/3/14
to ang...@googlegroups.com
I have a certain doc editor where user can edit existing docs or create new ones. An existing doc is accessed at URI:

/docs/1 (where 1 is doc id)

when user clicks 'create new doc' she lands on '/docs/new'.

At some point during edit of a new doc an ajax 'save' is triggered and the response contains new doc id. consequently, I'd like to update the browser location bar to show this id:

/docs/new --> /docs/1

...without a route update or re-instantiation of DocEditorCtrl or repainting the ng-view - so that user's editing is not interrupted.

I tried the following in DocEditorCtrl:

$scope.$on('$routeChangeStart',function(evt){

if($scope.blockRouteChange){
 evt.preventDefault();
 $scope.blockRouteChange = false;
}
});

$scope.onSave = function(docId)
{

$scope.blockRouteChange = true;
$location.path('/docs/'+docId);
};

...but the controller is re-instantiated. Is there a way to accomplish the above?

thanks,

-nikita

PS. I am aware that i can use reloadOnSearch=false with $routeProvider, but would prefer to keep docId as part of URI path rather than of query params.

Sander Elias

unread,
Jun 4, 2014, 1:54:22 AM6/4/14
to ang...@googlegroups.com
Hi Nikita,

What you want is only possible if you enable the HTML5 mode in the $location service.
have a look at the documentation for details.

Regards
Sander

Nikita Tovstoles

unread,
Jun 4, 2014, 2:14:25 AM6/4/14
to ang...@googlegroups.com

Hi, Sander:

I already have html 5 mode enabled and reviewed $location docs. Unfortunately, did not see anything that addresses my use case specifically.

Could you please elaborate?

--
You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/gYocaJZn3es/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Sander Elias

unread,
Jun 4, 2014, 3:09:26 AM6/4/14
to ang...@googlegroups.com
Hi Nikita,

I just know it is possible, I have not done it myself yet. It is not just flipping some switch and be done with it.
To get this working you need to study the HTML5 history api. this article might help to. Then apply
that knowledge to your app. Perhaps you even need to fiddle a bit with angular itself too to get what you want.

Just looked in a bit deeper, and I still think it is possible with the tools angular hands you already. 
check the $routeUpdate event. 

Regards
Sander

Reply all
Reply to author
Forward
0 new messages