Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
davideanderson opened this issue Jun 23, 2015 · 5 comments
Open

Comments

@davideanderson
Copy link

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.

@apparentlymart
Copy link
Owner

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?

@davideanderson
Copy link
Author

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.

@fracz
Copy link

fracz commented Aug 10, 2015

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.

@pelepelin
Copy link

pelepelin commented Jul 8, 2016

5226892 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.

@luishmcmoreno
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants