angularjs-viewhead icon indicating copy to clipboard operation
angularjs-viewhead copied to clipboard

Title is not retained in browser history when used with ui-router

Open davideanderson opened this issue 10 years ago • 5 comments

I am using angular-ui/ui-router (https://github.com/angular-ui/ui-router). The 'destroy' action of the directive occurs prior to ui-router registering the name in the history stack. Therefore, you cannot see the titles in this browser history. I have solved the issue in my project via a workaround. However, the workaround it is not as clean or modular as I would prefer, so I am not going to submit a pull request with it. I did, however, think I should report the issue, in case you can come up with a clean way of handling the issue.

davideanderson avatar Jun 23 '15 16:06 davideanderson

Thanks for the note, David.

I'm not too familiar with ui-router so I'd love to see some more details about your workaround so I can think about how it might be solved. Would you mind sharing some code here in the comments or in a gist?

apparentlymart avatar Jun 23 '15 16:06 apparentlymart

Here is a plunkr with how we use ui-router with angularjs-viewhead. Unfortunately you can't see history or browser title behavior in plunkr.
http://plnkr.co/edit/alLL86MllUecexa6iYWw

However, the way I solved the issue in my app is like this:

Rather than delete $rootScope.viewTitle in the title, I instead set the value $rootScope.viewTitleStale = true;
I also added $rootScope.viewTitleStale = false; where the viewTitle is assigned.

Finally, I added the following event watcher:

$rootScope.$on('$locationChangeSuccess', function() {
  if ($rootScope.viewTitleStale && $rootScope.viewTitle) {
    delete $rootScope.viewTitle;
  }
});

The watcher basically waits for the ui-router to do a $location change, and only then do I delete the viewTitle.

Though, I noticed when creating the plunkr, that you have made some changes since the release my bower is pulling in (0.0.1). Are you planning on putting out another release with your new changes? However, I tried doing a similar delayed delete myself, and it didn't seem to work, so I am pretty sure the issue is still there.

davideanderson avatar Jun 23 '15 19:06 davideanderson

Actually, the fix for it is not to clear the $rootScope.viewTitle on scope's $destroy event. A one little drawback is that title won't change if your new view does not declare one. But if you have <view-title> on every view, it should not be an issue.


EDIT: Actually, when you use master's version (with clearing the title in $timeout), angular-ui-router also works as expected.

fracz avatar Aug 10 '15 11:08 fracz

5226892f does not work for me on angular 1.3 and angular-ui-router 0.2.18. For some reason new view is created before "destroy" event of old view, so 'title = undefined' removes just written title and destroy event always removes the viewTitle.

I've fixed it by storing the iElement at the start of link function and comparing it with the element in the closure after the timeout in the destroy function. If the elements are identical, that means no other view-title was parsed since this view-title, and so it should be cleared. Otherwise, another view-title is already in charge, and so do nothing.

pelepelin avatar Jul 08 '16 15:07 pelepelin

With ui-router, i've fixed changing "$destroy" to "$stateChangeSuccess".

luishmcmoreno avatar Jan 08 '17 02:01 luishmcmoreno