ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Directive on ui-view is destroyed & re-linked on state-change

Open rubenswieringa opened this issue 11 years ago • 8 comments

In short: When you have a directive on a named ui-view element, then the directive will be destroyed and re-linked when navigating to or from a state that absolute-references that named ui-view.

Setup:

  • A html-file with two (sibling-) ui-views; one default, one named
  • A custom directive on the named ui-view element
  • Add 2 simple states that only use the default ui-view
  • Add 1 state that uses the default ui-view and absolute-references the named ui-view

Steps to reproduction:

  • Navigate to one of the ‘simple’ states
  • Navigate to the state that references the named ui-view
  • Navigate back to one of the ‘simple’ states

Expected behavior: › The custom directive on the named ui-view element should be linked at the start of the application, and in this scenario not be destroyed (ever)

Actual behavior: › The custom directive is destroyed (eg its $destroy callback is called) and linked (link() is called) when navigating to or from a state that absolute-references the named ui-view. When navigating between the 2 simple states, this doesn't occur.

Plunkr: http://plnkr.co/edit/pFKLCnYPoRXbaAkPL4uM?p=preview (let me know if you'd rather read normal, non-coffee javascript)

rubenswieringa avatar Aug 19 '15 08:08 rubenswieringa

Another side-effect of this problem is that jQuery.cleanData is run after linking the second scope, but before any watches set up by the link have fired. This causes interesting errors if using things like jSignature if you try and use them in the watch.

http://plnkr.co/edit/1pbKhsDZtGaC4MNyFOJy?p=preview

chilversc avatar Sep 09 '15 09:09 chilversc

+1 This is odd behavior and can be nasty in some situations.

jiverson avatar Sep 25 '15 04:09 jiverson

+1

demsey2 avatar Oct 21 '15 22:10 demsey2

+1 Also experiencing this..

tastycode avatar Dec 09 '15 01:12 tastycode

This is pretty counterintuitive to the idea that a link function should only run once. For anyone who is doing any sort of "one time setup" for a directive in their linking function they now have to implement some way of detecting if it's already been ran or not.

chrisfosterelli avatar Dec 15 '15 01:12 chrisfosterelli

+1

dracula92107 avatar Apr 29 '16 02:04 dracula92107

+1

eduardolagares avatar Mar 17 '18 04:03 eduardolagares

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues may be reopened.

Thank you for your contributions.

stale[bot] avatar Jan 24 '20 04:01 stale[bot]